【HDOJ】1987 Decoding

简单搜索。

 1 /* hdoj 1987 */
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 char s[505];
10 char map[25][25];
11 bool visit[25][25];
12 int n, m, len;
13 int dir[4][2] = {
14     0,1, 1,0, 0,-1, -1,0
15 }; // right down left up
16 
17 bool check(int x, int y) {
18     return x<0 || x>=n || y<0 || y>=m;
19 }
20 
21 void solve() {
22     int i, j, k, tmp;
23     int d = 0;
24     int t = n*m/5;
25     int x, y, xx, yy;
26     
27     len = 0;
28     memset(visit, false, sizeof(visit));
29     
30     x = 0, y = -1;
31     while (t--) {
32         k = 5;
33         tmp = 0;
34         while (k) {
35             xx = x + dir[d][0];
36             yy = y + dir[d][1];
37             if (check(xx, yy) || visit[xx][yy]) {
38                 d = (d+1) & 3;
39                 continue;
40             }
41             visit[xx][yy] = true;
42             tmp = (tmp<<1) + map[xx][yy]-'0';
43             --k;
44             x = xx;
45             y = yy;
46         }
47         if (tmp == 0)
48             s[len++] = ' ';
49         else
50             s[len++] = tmp + 'A' - 1;
51     }
52 }
53 
54 int main() {
55     int t, tt;
56     int i, j, k;
57     
58     #ifndef ONLINE_JUDGE
59         freopen("data.in", "r", stdin);
60     #endif
61     
62     scanf("%d", &t);
63     for (tt=1; tt<=t; ++tt) {
64         scanf("%d %d %s", &n, &m, s);
65         k = 0;
66         for (i=0; i<n; ++i)
67             for (j=0; j<m; ++j)
68                 map[i][j] = s[k++];
69         solve();
70         i = len - 1;
71         while (s[i] == ' ')
72             --i;
73         s[i+1] = '\0';
74         i = 0;
75         while (s[i] == ' ')
76             ++i;
77         printf("%d %s\n", tt, s+i);
78     }
79     
80     return 0;
81 }

 

posted on 2015-01-26 20:42  Bombe  阅读(153)  评论(0编辑  收藏  举报

导航