BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门

 1 /*
 2     BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到。因为雕像最多8步就会全部下落,
 3         只要撑过这个时间就能win,否则lose
 4 */
 5 #include <cstdio>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <vector>
 9 #include <cstring>
10 using namespace std;
11 
12 const int MAXN = 10;
13 const int INF = 0x3f3f3f3f;
14 struct Point{
15     int x, y, step;
16 };
17 char maze[MAXN][MAXN];
18 bool vis[MAXN][MAXN][MAXN];
19 int n;
20 
21 bool check(int x, int y, int s)    {
22     if (x >= 1 && x <= 8 && y >= 1 && y <= 8 && maze[x-s][y] != 'S')   return true;
23     return false;
24 }
25 
26 bool BFS(void)  {
27     queue<Point> Q;   Q.push ((Point) {8, 1, 0});
28     memset (vis, false, sizeof (vis));
29 
30     while (!Q.empty ()) {
31         int x = Q.front ().x, y = Q.front ().y, s = Q.front ().step;    Q.pop ();
32 
33         if (s > 8)  return true;
34         if (maze[x-s][y] == 'S')    continue;
35 
36         for (int i=-1; i<=1; ++i)   {
37             for (int j=-1; j<=1; ++j)   {
38                 int tx = x + i; int ty = y + j;
39                 if (!check (tx, ty, s))    continue;
40                 if (!vis[tx][ty][s+1])  {
41                     vis[tx][ty][s+1] = true;
42                     Q.push ((Point) {tx, ty, s + 1});
43                 }
44             }
45         } 
46     }
47 
48     return false;
49 }
50 
51 int main(void)  {       //Codeforces Beta Round #94 (Div. 2 Only) C. Statues
52     //freopen ("B.in", "r", stdin);
53 
54     n = 8;
55     while (scanf ("%s", maze[1] + 1) == 1)  {
56         for (int i=2; i<=n; ++i)    {
57             scanf ("%s", maze[i] + 1);
58         }
59         
60         if (BFS ()) puts ("WIN");
61         else    puts ("LOSE");
62     }
63 
64     return 0;
65 }
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4661332.html