【POJ】1204 Word Puzzles

这道题目各种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

时间: 2024-09-29 04:11:06

【POJ】1204 Word Puzzles的相关文章

POJ 题目1204 Word Puzzles(AC自动机,多个方向查询)

Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10244   Accepted: 3864   Special Judge Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started u

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

POJ 1204 Word Puzzles AC自动机题解

AC自动机的灵活运用,本题关键是灵活二字. 因为数据不是很大,时间要求也不高的缘故,所以本题有人使用暴力法也过了,有人使用Trie也过了. 当然有人使用AC自动机没AC的,在讨论区里喊AC自动机超时的,那是因为不会灵活运用,或者是硬套模板的,AC了速度也不会快. 给出本人的算法思路: 1 把需要查找的关键字建立Trie, 然后构造AC自动机 2 查找的时候分八个方向查找,比如棋盘是board[N][M],那么就可以循环i(0->N-1),然后每次把board[i]当做一个文本,做过HDU的key

【POJ】2278 DNA Sequence

各种wa后,各种TLE.注意若AC非法,则ACT等一定非法.而且尽量少MOD. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 105 8 #define NXTN 4 9 10 char str[15]; 11 12 typedef struct Matrix {

【POJ】1739 Tony&#39;s Tour

http://poj.org/problem?id=1739 题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数.(n,m<=8) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; #define BIT(a,b) ((a)<<((b)<<1)) #

【POJ】2449 Remmarguts&#39; Date(k短路)

http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k 首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h 根据f=g+h 我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路) 将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

【POJ】1056 IMMEDIATE DECODABILITY

字典树水题. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 typedef struct Trie { 6 bool v; 7 Trie *next[2]; 8 } Trie; 9 10 Trie *root; 11 12 bool create(char str[]) { 13 int i = 0, id; 14 bool ret = false; 15 Trie *p = root