【BFS】UVa1600 - Patrol Robot

题意

机器人从m*n的网格左上角(1,1)走到右下角最短所要走的步数,网格中0表示空地,1表示障碍,机器人最多可连续穿过k个障碍。

思路

BFS求最短路,稍微多了一点的是需要记录目前所连续穿过的障碍

总结

稍微加点东西就不会自己写了 ,多练啊还是

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 const int maxn = 25;
 8 using namespace std;
 9 const int dx[] = {-1, 0, 1,  0};
10 const int dy[] = { 0, 1, 0, -1};
11 int T, m, n, k;
12 int G[maxn][maxn], vis[maxn][maxn][maxn];
13 struct Node
14 {
15     int x, y;
16     int step, cur;
17     Node(int x, int y, int step, int cur):x(x),y(y),step(step),cur(cur){}
18 };
19 int bfs(Node t)
20 {
21     queue<Node>q;
22     q.push(t);
23     while(!q.empty()){
24         Node u = q.front();
25         q.pop();
26         if(u.x == n-1 && u.y == m-1) return u.step;
27         for(int i = 0; i < 4; i++){
28             int x = u.x + dx[i], y = u.y + dy[i], cur = u.cur;
29             if(G[x][y]) cur++;
30             else cur = 0;
31             if(cur <= k && !vis[x][y][cur] && x >= 0 && x < n && y >= 0 && y < m){
32                 vis[x][y][cur] = 1;
33                 q.push(Node(x, y, u.step+1, cur));
34             }
35         }
36     }
37     return -1;
38 }
39 int main()
40 {
41   //  freopen("in.txt","r",stdin);
42     cin >> T;
43     while(T--){
44         memset(vis, 0 , sizeof vis);
45         memset(G, 0 ,sizeof G);
46         cin >> n >> m >> k;
47         for(int i = 0; i < n; i++)
48             for(int j = 0; j < m; j++)
49                 cin >> G[i][j];
50         vis[0][0][0] = 1;
51         cout << bfs(Node(0,0,0,0)) << endl;
52     }
53     return 0;
54 }

 

posted @ 2016-10-27 15:13  kikii233  阅读(117)  评论(0编辑  收藏  举报