预处理+广搜(BFS)~
View Code
1 #include <cstdio>
2 #include <cstring>
3
4 #define OO(i) ((i & 1) ? (i - 1) : (i + 1))
5
6 const int SIZE = 64;
7 const int TIME = 1024;
8
9 const int dx[] = {-1,1,0,0,0};
10 const int dy[] = {0,0,-1,1,0};
11
12 const int mdx[4][9] =
13 {
14 {0,-1,-1,-1,-2,-2,-2,-2,-2},
15 {0,1,1,1,2,2,2,2,2},
16 {0,-1,0,1,-2,-1,0,1,2},
17 {0,-1,0,1,-2,-1,0,1,2}
18 };
19 const int mdy[4][9] =
20 {
21 {0,-1,0,1,-2,-1,0,1,2},
22 {0,-1,0,1,-2,-1,0,1,2},
23 {0,-1,-1,-1,-2,-2,-2,-2,-2},
24 {0,1,1,1,2,2,2,2,2}
25 };
26
27 int hi;
28 int wi;
29 int nk;
30
31 int endx;
32 int endy;
33
34 char board[SIZE][SIZE];
35
36 struct LL
37 {
38 int x;
39 int y;
40 int d;
41 };
42
43 struct QQ
44 {
45 int x;
46 int y;
47 int s;
48 };
49
50 LL monster[SIZE];
51
52 QQ queue[TIME * SIZE * SIZE];
53
54 bool visited[TIME][SIZE][SIZE];
55
56 bool see[TIME][SIZE][SIZE];
57
58 bool legal(int x,int y)
59 {
60 return x >= 0 && x < hi && y >= 0 && y < wi && board[x][y] != '*';
61 }
62
63 void process()
64 {
65 memset(see,false,sizeof(see));
66 for (int ti = 0;ti < TIME;ti++)
67 for (int i = 0;i < nk;i++)
68 {
69 int px = monster[i].x;
70 int py = monster[i].y;
71 int pd = monster[i].d;
72 for (int j = 0;j < 9;j++)
73 {
74 int nx = px + mdx[pd][j];
75 int ny = py + mdy[pd][j];
76 if (legal(nx,ny))
77 see[ti][nx][ny] = true;
78 }
79
80 int tx = px + dx[pd];
81 int ty = py + dy[pd];
82 if (!legal(tx,ty))
83 monster[i].d = OO(pd);
84 else
85 {
86 monster[i].x = tx;
87 monster[i].y = ty;
88 }
89 }
90 }
91
92 int bfs(int sx,int sy)
93 {
94 if(see[0][sx][sy]) return -1;
95
96 int qh = -1;
97 int qe = 0;
98
99 memset(visited,false,sizeof(visited));
100
101 queue[0].x = sx;
102 queue[0].y = sy;
103 queue[0].s = 0;
104
105 visited[0][sx][sy] = true;
106
107 while (qh != qe)
108 {
109 qh++;
110
111 int px = queue[qh].x;
112 int py = queue[qh].y;
113 int ps = queue[qh].s;
114
115 if (ps > 1000) return -1;
116 if (px == endx && py == endy) return ps;
117
118 for (int i = 0;i < 5;i++)
119 {
120 int nx = px + dx[i];
121 int ny = py + dy[i];
122 int ns = ps + 1;
123
124 if (!legal(nx,ny)) continue;
125 if(see[ns][nx][ny]) continue;
126 if (visited[ns][nx][ny]) continue;
127
128 qe++;
129 queue[qe].x = nx;
130 queue[qe].y = ny;
131 queue[qe].s = ns;
132 visited[ns][nx][ny] = true;
133 }
134 }
135
136 return -1;
137 }
138
139 int main()
140 {
141 int cas;
142 int stx;
143 int sty;
144
145 scanf("%d",&cas);
146 for (int cc = 1;cc <= cas;cc++)
147 {
148 scanf("%d %d",&hi,&wi);
149 for (int i = 0;i < hi;i++)
150 scanf("%s",board[i]);
151 scanf("%d",&nk);
152 for (int i = 0;i < nk;i++)
153 {
154 scanf("%d %d %d",&monster[i].x,&monster[i].y,&monster[i].d);
155 monster[i].x--;
156 monster[i].y--;
157 monster[i].d--;
158 }
159
160 for (int i = 0;i < hi;i++)
161 for (int j = 0;j < wi;j++)
162 if (board[i][j] == 'A')
163 stx = i, sty = j;
164 else if (board[i][j] == 'B')
165 endx = i, endy = j;
166
167 process();
168
169 printf("Case %d: ",cc);
170
171 int step = bfs(stx,sty);
172 if (step == -1) printf("胜败兵家事不期 卷土重来是大侠\n");
173 else printf("%d\n",step);
174 }
175 return 0;
176 }