UVa1600 Patrol Robot (最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/49764分析:多了一个不能连续穿过k个障碍物的限制条件,那么就在状态中增加一个表示已连续穿过cnt个障碍物的描述,vis也要增加一维变成vis[x][y][cnt],毕竟vis用来记录当前状态是否曾经已经出现过。碰到障碍物且拿来扩展的状态cnt小于等于k就将cnt++,否则遇到空地就将cnt清零。
 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 20 + 5;
 7
 8 int G[maxn][maxn], vis[maxn][maxn][maxn], d[maxn][maxn];
 9
10 struct Node {
11     int x, y, cnt;
12 };
13
14 const int dx[4] = {0, 0, 1, -1};
15 const int dy[4] = {1, -1, 0, 0};
16
17 int main() {
18     //freopen("slyar.in", "r", stdin);
19     //freopen("slyar.out", "w", stdout);
20     int T;
21     scanf("%d", &T);
22     while (T--) {
23         int m, n, k;
24         scanf("%d%d%d", &m, &n, &k);
25         memset(G, 0, sizeof(G));
26         for (int i = 1; i <= m; i++)
27             for (int j = 1; j <= n; j++)
28                 scanf("%d", &G[i][j]);
29         memset(vis, 0, sizeof(vis));
30         memset(d, -1, sizeof(d));
31         d[1][1] = 0; vis[1][1][0] = 1;
32         Node t; t.x = 1, t.y = 1, t.cnt = 0;
33         queue<Node> q;
34         q.push(t);
35         while (!q.empty()) {
36             Node u = q.front(); q.pop();
37             //  printf("x = %d, y = %d, cnt = %d\n", u.x, u.y, u.cnt);
38             if (u.x == m && u.y == n) break;
39             for (int i = 0; i < 4; i++) {
40                 int nx = u.x + dx[i], ny = u.y + dy[i];
41                 if (nx >= 1 && nx <= m && ny >= 1 && ny <= n) {
42                      if (G[nx][ny] == 1 && u.cnt + 1 > k) continue;
43                      Node v = u; v.x = nx, v.y = ny;
44                      if (G[nx][ny] == 1) v.cnt++; else v.cnt = 0;
45                      if (vis[nx][ny][v.cnt]) continue; else vis[nx][ny][v.cnt] = 1;
46                      d[nx][ny] = d[u.x][u.y] + 1;
47                      q.push(v);
48                 }
49             }
50         }
51         printf("%d\n", d[m][n]);
52     }
53     return 0;
54 }
时间: 2024-08-27 20:15:33

UVa1600 Patrol Robot (最短路)的相关文章

UVA1600 Patrol Robot

题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace std;struct node{ int x,y,c

UVA 1600 Patrol Robot(BFS扩展)

Patrol Robot Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m.

UVA 1600 Patrol Robot

带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 7 const int maxn = 30 ; 8 9 struct node { 10 int x,y; 11 int

UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

UVA 1600 Patrol Robot Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The colu

Uva 1600 Patrol Robot (BFS 最短路/DFS剪枝)

这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #include <bits/stdc++.h> using namespace std; struct Node{ int r; int c; int g; int cnt; Node(int r,int c,int g,int cnt):r(r),c(c),g(g),cnt(cnt){} }; int v

Patrol Robot UVa1600巡逻机器人

题意大概: 机器人要从一个m*n(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n).网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示.机器人每次可以往四个方向走一格,但不能连续地穿越k( [0,20] )个障碍,求最短路长度.起点和终点保证是空地. 思路:用bfs搜索即可,由于不能连续地穿越k个障碍,所以在原本的vis2维数组上面再添加1维,变成3维数组,表示穿越的墙的层数(障碍). 代码如下: #include<cstdio> #include<

UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS) 解题心得

原题: Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j) denotes the cell in row i and column j in the

Patrol Robot UVA - 1600

题意:给一个矩阵,从(1,1)走到(m,n)的最短路,"1"是障碍,不能连续穿过k个障碍. WA了16次,花费了一个上午和半个下午...想?? 每个点的属性至少是3维,比如每个点带有的已经连续穿过障碍的次数,再把跑过的步长算上,就四维了,所以最好用一个结构体! AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5

UVa 1600 Patrol Robot (习题 6-5)

传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0,当前的k变成最初的k. vis[x][y][z]  x, y代表坐标  z表示k  当为真时说明该点剩余穿墙次数为k的状态已出现过 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef struct