输入的第一行为一个正整数T,表示测试数据组数。 接下来有T组数据。每组数据的第一行包括两个整数m和n,表示字符迷阵的行数和列数。接下来有m行,每一行为一个长度为n的字符串,按顺序表示每一行之中的字符。再接下来还有一行包括一个字符串,表示要寻找的单词。 数据范围: 对于所有数据,都满足1<=T<=9,且输入的所有位于字符迷阵和单词中的字符都为大写字母。要寻找的单词最短为2个字符,最长为9个字符。字符迷阵和行列数,最小为1,最多为99。 对于其中50%的数据文件,字符迷阵的行列数更限制为最多为20。
对于每一组数据,输出一行,包含一个整数,为在给定的字符迷阵中找到给定的单词的合法方案数。
3 10 10 AAAAAADROW WORDBBBBBB OCCCWCCCCC RFFFFOFFFF DHHHHHRHHH ZWZVVVVDID ZOZVXXDKIR ZRZVXRXKIO ZDZVOXXKIW ZZZWXXXKIK WORD 3 3 AAA AAA AAA AA 5 8 WORDSWOR ORDSWORD RDSWORDS DSWORDSW SWORDSWO SWORD
4 16 5
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <cstring>
using namespace std;
int m, n;
char wordmap[100][100];
char seekword[100];
int len_seekword;
int x[] = {1, 0, 1};
int y[] = {0, 1, 1};
int ans;
void deep_search_first()
{
int i, j, k;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(wordmap[i][j] == seekword[0]) {
//探索三个方向是否存在合法单词
int cur_pos;
int heng;
int zong;
for(k=0; k<3; k++) {
heng = i + x[k];
zong = j + y[k];
cur_pos = 1;
while(heng<m && zong<n && cur_pos<len_seekword && wordmap[heng][zong]==seekword[cur_pos]) {
heng = heng + x[k];
zong = zong + y[k];
cur_pos++;
}
if(cur_pos == len_seekword) {
ans++;
}
}
}
}
}
}
int main()
{
int group;
scanf("%d", &group);
while(group--) {
scanf("%d %d%*c", &m, &n);
for(int i=0; i<m; i++) {
scanf("%s", wordmap[i]);
}
scanf("%s", seekword);
len_seekword = strlen(seekword);
ans = 0;
deep_search_first();
printf("%d\n", ans);
}
}
原文:https://www.cnblogs.com/yspworld/p/10502583.html