就是用字典树,然后用stl的map几个方向搜一遍就行拉
代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn = 26;
char map[1005][1005], word[1005][1005];
int ans[1005][3];
int l, c, w;
bool vis[1005];
int dir[8][2] = { {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1} };
struct Trie
{
int n;
Trie *next[maxn];
};
Trie *root;
void init()
{
root = (Trie *)malloc(sizeof(Trie));
for(int i = 0; i < maxn; i++)
{
root -> next[i] = NULL;
root -> n = -1;
}
}
void insert(char *word, int id)
{
Trie *temp = root;
for(int i = 0; i < strlen(word); i++)
{
int pos = word[i] - 'A';
if(temp -> next[pos] == NULL)
{
Trie *cur = (Trie *)malloc(sizeof(Trie));
for(int j = 0; j < maxn; j++)
{
cur -> next[j] = NULL;
cur -> n = -1;
}
temp -> next[pos] = cur;
}
temp = temp -> next[pos];
}
temp -> n = id;
}
void search(int x, int y, int d)
{
Trie *temp = root;
int xx = x, yy = y;
while(xx >= 0 && xx < l && yy >= 0 && yy < c)
{
if(temp -> next[ map[xx][yy] - 'A' ] == NULL)
break;
else
temp = temp -> next[ map[xx][yy] - 'A' ];
if(temp -> n != -1)
{
if(!vis[temp -> n])
{
vis[temp -> n] = 1;
ans[temp -> n][0] = x;
ans[temp -> n][1] = y;
ans[temp -> n][2] = d;
}
}
xx += dir[d][0];
yy += dir[d][1];
}
}
int main()
{
scanf("%d%d%d", &l, &c, &w);
for(int i = 0; i < l; i++)
scanf("%s", map[i]);
init();
memset(vis, 0, sizeof(vis));
for(int i = 0; i < w; i++)
{
scanf("%s", word[i]);
insert(word[i], i);
}
for(int i = 0; i < l; i++)
for(int j = 0; j < c; j++)
for(int k = 0; k < 8; k++)
search(i, j, k);
for(int i = 0; i < w; i++)
printf("%d %d %c\n", ans[i][0], ans[i][1], ans[i][2] + 'A');
return 0;
}