CodeForces

这道题是一道搜索题 

但是 如果没有读懂或者 或者拐过弯 就很麻烦

最多26个火车 那么每一个周期 (人走一次 车走一次) 就要更改地图 的状态 而且操作复杂 容易超时 出错

利用相对运动 

计周期为 人向上或向下或不动一步 + 向右三步

这样就变为走迷宫问题了

同样要注意

1、去重数组 或者 将以前访问过的点置为其他符号 避免重复访问

2、还有 因为 每次是三步三步的往右走 所以最后的边界可能不够 可以再扩充三列

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 typedef pair<int, int> P;
 9 
10 int T,t;
11 //bool vis[3][104];
12 char maze[3][104];
13 P start;
14 
15 bool check(int x ,int y)
16 {
17     if (x > 2 || x < 0) return false;
18     if (maze[x][y] != '.') return false;
19     return true;
20 }
21 bool bfs()
22 {
23     queue<P> que;
24     P crt = start;
25     crt.second++;
26     if (crt.second >= t) return true;
27     if (maze[crt.first][crt.second] == '.') que.push(crt);
28     while (!que.empty())
29     {
30         P crt = que.front();
31         que.pop();
32         for (int i = -1; i < 2; i++)
33         {
34             int tx = crt.first + i, ty = crt.second;
35             if ( !check(tx, ty) ) continue;
36             bool isSafe = true;
37             for (int j = 1; j <= 3; j++)
38             {
39                 if (!check(tx, ty+j))
40                 {
41                     isSafe = false;
42                     break;
43                 }
44             }
45             if (isSafe)
46             {
47                 if (ty+3 >= t) return true;
48                 else
49                 que.push(P(tx, ty+3));
50             }
51         }
52         maze[crt.first][crt.second] = 'A';//使用去重数组的话 内存超了 当访问完这个点以后 做标记 以后不再访问
53     }
54     return false;
55 }
56 int main()
57 {
58     freopen("in.txt", "r", stdin);
59     scanf("%d", &T);
60     int k;
61     while (T--)
62     {
63         scanf("%d%d", &t, &k);
64         //memset(vis, false, sizeof(vis));
65         for (int i = 0; i < 3; i++)
66         {
67             getchar();
68             for (int j = 0; j < t; j++)
69             {
70                 scanf("%c", &maze[i][j]);
71                 if (maze[i][j] == 's')
72                 {
73                     start.first = i;
74                     start.second = j;
75                 }
76             }
77             for (int j = t; j < t+3; j++)
78             {
79                 maze[i][j] = '.';
80             }
81         }
82         if(bfs()) printf("YES
");
83         else printf("NO
");
84     }
85     return 0;
86 }
原文地址:https://www.cnblogs.com/oscar-cnblogs/p/6337879.html