fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4.  
  5. int max(int a, int b)
  6. {
  7. if(a > b) return a;
  8. return b;
  9. }
  10.  
  11. int min(int a, int b)
  12. {
  13. if(a < b) return a;
  14. return b;
  15. }
  16.  
  17. void swap(int *a, int *b)
  18. {
  19. int tmp = *a;
  20. *a = *b;
  21. *b = tmp;
  22. }
  23.  
  24. int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
  25.  
  26. char a[200][200], s[100][100], input[100]; int n, m;
  27.  
  28.  
  29. struct Pos{
  30. int x, y, dir_x, dir_y;
  31. }pos[100];
  32.  
  33. void find(int sx, int sy, int i)
  34. {
  35. for(int d = 0; d < 8; ++ d){
  36. int nx = sx, ny = sy, j = 0; bool check = true;
  37. while(nx > 0 && ny > 0 && nx <= n && ny <= m && j < strlen(s[i])){
  38. if(a[nx][ny] != s[i][j]) { check = false; break; }
  39. nx += dx[d]; ny += dy[d];
  40. ++ j;
  41. }
  42. if(check && j == strlen(s[i])){
  43. pos[i] = (struct Pos){sx, sy, dx[d], dy[d]};
  44. return;
  45. }
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. if(fopen("1.inp", "r")){
  52. freopen("1.inp", "r", stdin);
  53. freopen("1.out", "w", stdout);
  54. }
  55. scanf("%d%d", &n, &m);
  56. for(int i = 1; i <= n; ++ i){
  57. fgets(input, sizeof(input), stdin);
  58. input[strcspn(input, "\n")] = '\0';
  59. for(int j = 0, k = 0; j < strlen(input); ++ j) if(input[j] != ' ') a[i][++ k] = input[j];
  60. }
  61.  
  62. for(int i = 1; i < 100; ++ i) pos[i] = (struct Pos){0, 0, 0, 0};
  63.  
  64. int num = 0;
  65. while(scanf("%s", s[++ num])){
  66. s[num][strcspn(s[num], "\n")] = '\0';
  67. if(s[num][0] == '#') break;
  68. }
  69. -- num;
  70.  
  71. for(int i = 1; i <= n; ++ i) for(int j = 1; j <= m; ++ j) for(int k = 1; k <= num; ++ k)
  72. find(i, j, k);
  73.  
  74. for(int i = 1; i <= num; ++ i){
  75. if(!pos[i].x) printf("%s %d\n", s[i], -1);
  76. else printf("%s %d %d %d %d\n", s[i], pos[i].x - 1, pos[i].y - 1, pos[i].dir_x, pos[i].dir_y);
  77. }
  78. // printf("-1");
  79. }
  80.  
Success #stdin #stdout 0s 5280KB
stdin
10 10
H  E  L  L  O  A  B  C  D  E
A  I  G  L  E  O  P  L  A  N
P  R  O  G  R  A  M  M  E  R
C  A  T  S  D  O  G  X  Y  Z
S  T  A  C  K  Q  U  E  U  E
E  D  C  B  E  E  U  G  R  E
R  Q  O  U  C  K  K  E  H  S
F  O  X  Y  Z  D  L  I  E  T
L  I  M  P  K  H  O  U  S  N
M  I  T  U  W  P  R  O  C  K
FELL
CAT
ROUGHT
BOOL
FALSE
MIXUE
#
stdout
FELL -1
CAT 5 2 -1 0
ROUGHT 2 4 1 1
BOOL 5 3 1 -1
FALSE -1
MIXUE 9 0 -1 1