开始没看清楚题意,把题看复杂了,于是使用了递归,写完后一看发现自己写的不就是以前自己没看懂的深度优先搜索(DFS)么?后来发现还真是,自己居然“发明”了DFS,而且用的还是递归栈,不禁得意,结果多次WA,不知为何。无奈之下只好拿别人的代码对拍,把样例数据加了点,发现自己的程序得出的结果居然比别人程序的结果多,再看题才发现这题原来那么白痴- -!但发现可以把DFS改一下完成题目,于是顺利AC。看来以后一定要读好题,不要自以为是啊- -!于是又写了个“正宗”的,代码在下面。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 60
int search(char list[][N], char word[], int row, int col, int m, int pstn, int n)
{
int l = strlen(list[row]), i, j;
if (word[pstn]) {
if ((!n || n == 1) && list[i=(row-1>=0?row-1:m-1)][col] == word[pstn]
&& search(list, word, i, col, m, pstn+1, 1)) /* up */
return 1;
else if ((!n || n == 2) && list[i=(row+1<m?row+1:0)][col] == word[pstn]
&& search(list, word, i, col, m, pstn+1, 2)) /* down */
return 1;
else if ((!n || n == 3) && list[row][j=(col-1>=0?col-1:l-1)] == word[pstn]
&& search(list, word, row, j, m, pstn+1, 3)) /* left */
return 1;
else if ((!n || n == 4) && list[row][j=(col+1<l?col+1:0)] == word[pstn]
&& search(list, word, row, j, m, pstn+1, 4)) /* right */
return 1;
else if ((!n || n == 5) && list[i=(row-1>=0?row-1:m-1)][j=(col-1>=0?col-1:l-1)] == word[pstn]
&& search(list, word, i, j, m, pstn+1, 5)) /* up-left */
return 1;
else if ((!n || n == 6) && list[i=(row-1>=0?row-1:m-1)][j=(col+1<l?col+1:0)] == word[pstn]
&& search(list, word, i, j, m, pstn+1, 6)) /* up-right */
return 1;
else if ((!n || n == 7) && list[i=(row+1<m?row+1:0)][j=(col-1>=0?col-1:l-1)] == word[pstn]
&& search(list, word, i, j, m, pstn+1, 7)) /* down-left */
return 1;
else if ((!n || n == 8) && list[i=(row+1<m?row+1:0)][j=(col+1<l?col+1:0)] == word[pstn]
&& search(list, word, i, j, m, pstn+1, 8)) /* down-right */
return 1;
return 0;
} else
return 1;
}
int main(void)
{
char list[N][N], words[N][N];
int i, j, k, z, m, n, cases;;
scanf("%d", &cases);
while (cases--) {
int found[N] = {0};
scanf("%d%d", &m, &n);
for (i = 0; i < m; ++i)
scanf("%s", list[i]);
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
list[i][j] = toupper(list[i][j]);
scanf("%d", &k);
for (i = 0; i < k; ++i)
scanf("%s", words[i]);
for (i = 0; i < k; ++i)
for (j = 0; j < strlen(words[i]); ++j)
words[i][j] = toupper(words[i][j]);
for (z = 0; z < k; ++z)
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (!found[z] && list[i][j] == words[z][0]
&& search(list, words[z], i, j, m, 1, 0)) {
printf("%d %d\n", i+1, j+1);
found[z] = 1;
}
if (cases)
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 60
int search(char list[][N], char word[], int row, int col, int m, int n, int len)
{
int i, j, k;
for (i = row, k = 0; i >= 0 && k < len && list[i][col] == word[k]; --i, ++k) ; /* up */
if (k == len) return 1;
for (i = row, k = 0; i < m && k < len && list[i][col] == word[k]; ++i, ++k) ; /* down */
if (k == len) return 1;
for (j = col, k = 0; j >= 0 && k < len && list[row][j] == word[k]; --j, ++k) ; /* left */
if (k == len) return 1;
for (j = col, k = 0; j < n && k < len && list[row][j] == word[k]; ++j, ++k) ; /* right */
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j >= 0 && k < len /* up-left */
&& list[i][j] == word[k]; i--, j--, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i >= 0 && j < n && k < len /* up-right */
&& list[i][j] == word[k]; i--, j++, ++k) ;
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j >= 0 && k < len
&& list[i][j] == word[k]; i++, j--, ++k) ; /* down-left */
if (k == len) return 1;
for (i = row, j = col, k = 0; i < m && j < n && k < len
&& list[i][j] == word[k]; i++, j++, ++k) ; /* down-right */
return k == len;
}
int main(void)
{
char list[N][N], words[N][N];
int i, j, k, z, m, n, cases;;
scanf("%d", &cases);
while (cases--) {
int found[N] = {0};
scanf("%d%d", &m, &n);
for (i = 0; i < m; ++i)
scanf("%s", list[i]);
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
list[i][j] = toupper(list[i][j]);
scanf("%d", &k);
for (i = 0; i < k; ++i)
scanf("%s", words[i]);
for (i = 0; i < k; ++i)
for (j = 0; j < strlen(words[i]); ++j)
words[i][j] = toupper(words[i][j]);
for (z = 0; z < k; ++z)
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (!found[z] && search(list, words[z], i, j, m, n, strlen(words[z]))) {
printf("%d %d\n", i+1, j+1);
found[z] = 1;
}
if (cases)
printf("\n");
}
return 0;
}