-
Notifications
You must be signed in to change notification settings - Fork 2
/
10010.cpp
187 lines (143 loc) · 2.55 KB
/
10010.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAXN 10000
/* 10000 */
char GRID[MAXN][51];
char WORD[55];
int M,N,WW,R,C;
int HF(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++)
if(WORD[i] != GRID[r][c++]) return 0;
R = r;
C = c-1;
return 1;
}
int HB(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(c<0) return 0;
if(WORD[i] != GRID[r][c--]) return 0;
}
R = r;
C = c+1;
return 1;
}
int VF(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(r>=M) return 0;
if(WORD[i] != GRID[r++][c]) return 0;
}
R = r-1;
C = c;
return 1;
}
int VB(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(r<0) return 0;
if(WORD[i] != GRID[r--][c]) return 0;
}
R = r+1;
C = c;
return 1;
}
int DWN(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(r<0 ||c<0) return 0;
if(WORD[i] != GRID[r--][c--]) return 0;
}
R = r+1;
C = c+1;
return 1;
}
int DNE(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(r<0 || c>=N) return 0;
if(WORD[i] != GRID[r--][c++]) return 0;
}
R = r+1;
C = r-1;
return 1;
}
int DES(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(r>=M ||c>=N) return 0;
if(WORD[i] != GRID[r++][c++]) return 0;
}
R = r-1;
C = c-1;
return 1;
}
int DSW(int r,int c) {
int i,j;
for(i = 0; WORD[i]; i ++) {
if(r>=M ||c<0) return 0;
if(WORD[i] != GRID[r++][c--]) return 0;
}
R = r-1;
C = c+1;
return 1;
}
int CHECK(int r,int c) {
if(HF(r,c))
return 1;
else if(HB(r,c))
return 1;
else if(VF(r,c))
return 1;
else if(VB(r,c))
return 1;
else if(DWN(r,c))
return 1;
else if(DNE(r,c))
return 1;
else if(DES(r,c))
return 1;
else if(DSW(r,c))
return 1;
return 0;
}
int SOLVECASE() {
int p,q;
for(p = 0; p<M; p++)
for(q =0;q<N; q++)
if(WORD[0] == GRID[p][q])
if(CHECK(p,q))
goto done;
done:;
printf("%d %d\n",p+1,q+1);
return 0;
}
int main() {
int kase,i,n,m,f,j;
char input[50],fal[10];
gets(input);
sscanf(input,"%d",&kase);
f = kase;
while(kase --) {
gets(fal);
gets(input);
sscanf(input,"%d%d",&M,&N);
for(i = 0; i<M; i ++) {
gets(GRID[i]);
for(j = 0; GRID[i][j]; j ++)
GRID[i][j] = tolower(GRID[i][j]);
}
gets(input);
sscanf(input,"%d",&WW);
for(i = 0 ;i<WW; i++) {
gets(WORD);
for(j = 0; WORD[j]; j ++)
WORD[j] = tolower(WORD[j]);
SOLVECASE();
}
if(kase>0) printf("\n");
}
return 0;
}