码迷,mamicode.com
首页 > 其他好文 > 详细

【POJ】1204 Word Puzzles

时间:2014-07-07 18:07:44      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   io   div   

这道题目各种wa。首先是错了一个坐标,居然没测出来。然后是剪枝错误。搜索pen时就返回,可能还存在串pen*。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 
 5 #define MAXN 1005
 6 
 7 typedef struct Trie {
 8     int v;
 9     Trie *next[26];
10     Trie() {
11         v = 0;
12         for (int i=0; i<26; ++i)
13             next[i] = NULL;
14     }
15 } Trie;
16 
17 Trie root;
18 int direct[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
19 char map[MAXN][MAXN], buf[MAXN];
20 int ans[MAXN][3], n, m, xx, yy;
21 bool visit[MAXN];
22 
23 void create(char str[], int in) {
24     int i = 0, id;
25     Trie *p = &root, *q;
26 
27     while (str[i]) {
28         id = str[i] - A;
29         ++i;
30         if (p->next[id] == NULL) {
31             q = new Trie();
32             p->next[id] = q;
33         }
34         p = p->next[id];
35     }
36     p->v = in;
37 }
38 
39 void find(Trie *t, int x, int y, int k) {
40     if (t == NULL)
41         return ;
42 
43     if (t->v && !visit[t->v]) {
44         visit[t->v] = true;
45         ans[t->v][0] = xx;
46         ans[t->v][1] = yy;
47         ans[t->v][2] = k;
48     }
49     if (x<0 || x>=n || y<0 || y>=m)
50         return ;
51     find(t->next[map[x][y]-A], x+direct[k][0], y+direct[k][1], k);
52 }
53 
54 void search() {
55     int k;
56 
57     memset(visit, false, sizeof(visit));
58 
59     for (xx=0; xx<n; ++xx)
60         for (yy=0; yy<m; ++yy)
61             for (k=0; k<8; ++k)
62                 find(&root, xx, yy, k);
63 }
64 
65 int main() {
66     int w;
67     int i;
68 
69     scanf("%d%d%d",&n,&m,&w);
70 
71     for (i=0; i<n; ++i)
72         scanf("%s", map[i]);
73 
74     for (i=1; i<=w; ++i) {
75         scanf("%s", buf);
76         create(buf, i);
77     }
78     search();
79     for (i=1; i<=w; ++i)
80         printf("%d %d %c\n", ans[i][0], ans[i][1], ans[i][2]+A);
81 
82     return 0;
83 }

 

【POJ】1204 Word Puzzles,布布扣,bubuko.com

【POJ】1204 Word Puzzles

标签:style   blog   color   for   io   div   

原文地址:http://www.cnblogs.com/bombe1013/p/3813560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!