ZOJ3865:Superbot(BFS) The 15th Zhejiang University Programming Contest

一个有几个小坑的bfs

题目很长,但并不复杂,大概总结起来有这么点。

有t组输入

每组输入n, m, p。表示一个n*m的地图,每p秒按键会右移一次(这个等会儿再讲)。

然后是地图的输入。其中'@'为起点,'$'为终点,'.'为通路,'*'为不通。

问从起点到终点最少需要多久?

一眼看去,裸的bfs嘛,10*10的地图,简单!

不过还是连错4次……

 

注意!

每一秒有4种操作,注意,不是三种,1. 光标左移,2. 光标右移,3. 按照光标方向行走,4. 不动(尤其是这一个)。

所谓光标左移,右移,因为题目中给出了光标(在图上,共4个方向),每次改变的移动方向只能是当前光标选择的方向的左右两个。而行走只能按照光标所指的方向。

另外每过p秒,方向会变化(初始四个方向的顺序为左右上下,p秒后变成右上下左,再过p秒后变为上下左右)。

 

废话说完,上代码——

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 using namespace std;
  7 
  8 const int N = 20;
  9 
 10 struct node
 11 {
 12     int x, y, dis, step;
 13 };
 14 
 15 int go[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
 16 
 17 int t;
 18 int n, m, P;
 19 char mp[N][N];
 20 bool v[N][N];
 21 bool vv[N][N][5];
 22 
 23 int change(int a)
 24 {
 25     switch(a)
 26     {
 27     case 0:
 28         return 3;
 29     case 1:
 30         return 0;
 31     case 2:
 32         return 1;
 33     case 3:
 34         return 2;
 35     }
 36 }
 37 
 38 void bfs()
 39 {
 40     node p;
 41     bool k = 0;
 42     for(int i = 0; i < n; i++)
 43     {
 44         for(int j = 0; j < m; j++)
 45         {
 46             if(mp[i][j] == '@')
 47             {
 48                 p.x = i;
 49                 p.y = j;
 50                 k = 1; break;
 51             }
 52         }
 53         if(k) break;
 54     }
 55     p.dis = 0;
 56     p.step = 0;
 57     vv[p.x][p.y][0] = 1;
 58 
 59     queue<node> que;
 60     que.push(p);
 61     while(!que.empty())
 62     {
 63         node p = que.front();
 64         que.pop();
 65 
 66         int xx = p.x+go[p.dis][0];
 67         int yy = p.y+go[p.dis][1];
 68         if(xx >= 0 && xx < n && yy >= 0 && yy < m)
 69         {
 70             if(v[xx][yy] == 0)
 71             {
 72                 if(mp[xx][yy] == '.')
 73                 {
 74                     v[xx][yy] = 1;
 75                     node q;
 76                     q.x = xx;
 77                     q.y = yy;
 78                     q.step = p.step+1;
 79                     q.dis = p.dis;
 80                     if(q.step%P == 0)
 81                     {
 82                         q.dis = change(q.dis);
 83                         vv[xx][yy][q.dis] = 1;
 84                     }
 85                     que.push(q);
 86                 }
 87                 else if(mp[xx][yy] == '$')
 88                 {
 89                     printf("%d\n", p.step+1);
 90                     return;
 91                 }
 92             }
 93         }
 94 
 95         if((p.step+1)%P == 0) {p.dis = change(p.dis);}
 96 
 97         node q;
 98         q.x = p.x;
 99         q.y = p.y;
100         q.step = p.step+1;
101 
102         q.dis = p.dis+1;
103         q.dis %= 4;
104         if(vv[q.x][q.y][q.dis] == 0)
105         {
106             que.push(q);
107             vv[q.x][q.y][q.dis] = 1;
108         }
109 
110         q.dis = p.dis+3;
111         q.dis %= 4;
112         if(vv[q.x][q.y][q.dis] == 0)
113         {
114             que.push(q);
115             vv[q.x][q.y][q.dis] = 1;
116         }
117 
118         q.dis = p.dis;
119         if(vv[q.x][q.y][q.dis] == 0)
120         {
121             que.push(q);
122             vv[q.x][q.y][q.dis] = 1;
123         }
124     }
125     printf("YouBadbad\n");
126 }
127 
128 int main()
129 {
130     //freopen("test.txt", "r", stdin);
131     scanf("%d", &t);
132     while(t--)
133     {
134         memset(mp, 0, sizeof(mp));
135         memset(v, 0, sizeof(v));
136         memset(vv, 0, sizeof(vv));
137         scanf("%d%d%d", &n, &m, &P);
138         for(int i = 0; i < n; i++)
139         {
140             scanf("%s", mp[i]);
141         }
142         bfs();
143     }
144 }

 

posted @ 2015-05-09 22:32  mypride  阅读(265)  评论(0编辑  收藏  举报