描述

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 grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from (x, y) to (x + 1, y), (x, y + 1), (x - 1, y) or (x, y - 1). Some of the cells in the grid contain obstacles. In order to move to a cell containing obstacle, the robot has to switch to turbo mode. Therefore, the robot cannot move continuously to more than k cells containing obstacles.

Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell (m, n). It is assumed that both these cells do not contain obstacles.

Input

The input consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data set, the first line contains two positive integer numbers m and n separated by space (1m, n20). The second line contains an integer number k(0k20). The ith line of the next m lines contains n integer aij separated by space (i = 1, 2,..., m;j = 1, 2,..., n). The value of aij is 1 if there is an obstacle on the cell (i, j), and is 0 otherwise.

Output

For each data set, if there exists a way for the robot to reach the cell (m, n), write in one line the integer number s, which is the number of moves the robot has to make; -1 otherwise.

Sample Input

  1. 3
  2. 2 5
  3. 0
  4. 0 1 0 0 0
  5. 0 0 0 1 0
  6. 4 6
  7. 1
  8. 0 1 1 0 0 0
  9. 0 0 1 0 1 1
  10. 0 1 1 1 1 0
  11. 0 1 1 1 0 0
  12. 2 2
  13. 0
  14. 0 1
  15. 1 0

Sample Output

  1. 7
  2. 10
  3. -1

  4. ***解题思路:

用一个vis[x][y][z]表示走到x,y的时候 穿过了z个墙,标记现在的步数

进行递归的条件是,走到下一步时候,之前走到这里的步数必须下与之后走到这里的步数。

  1. 程序代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <queue>
  7. #define maxn 20+5
  8. using namespace std;
  9. int map[maxn][maxn],vis[maxn][maxn][maxn];
  10. int m,n,k,ans;
  11. int dx[]={,,,-};
  12. int dy[]={,-,,};
  13. struct Node{
  14. int x,y;
  15. int cnt;
  16. int k;
  17. };
  18.  
  19. int bfs(){
  20. queue<Node> q;
  21. Node u;
  22. u.x=;u.y=;u.cnt=;u.k=k;
  23. vis[][][k]=;
  24. q.push(u);
  25. while (!q.empty()){
  26. u=q.front();q.pop();
  27. if (u.x==n-&&u.y==m-){
  28. ans=u.cnt;
  29. return ;
  30. }
  31. Node v;
  32. if (u.k>=)//如果到该点没有命了, 那么就不需要在该点扩展了
  33. for (int i=;i<;i++){
  34. v.x=u.x+dx[i];
  35. v.y=u.y+dy[i];
  36. v.cnt=u.cnt+;
  37. if (map[v.x][v.y]) v.k=u.k-;
  38. else v.k=k;//碰到0就恢复满命
  39. if (v.x>=&&v.x<n&&v.y>=&&v.y<m&&!vis[v.x][v.y][v.k]){
  40. if (v.k>=) {q.push(v);vis[v.x][v.y][v.k]=;}
  41. }
  42. }
  43. }
  44. if (q.empty()) ans=-;
  45. }
  46. int main()
  47. {
  48. int T;
  49. cin>>T;
  50. while (T--){
  51. memset(map,,sizeof(map));
  52. memset(vis,,sizeof(vis));
  53. cin>>n>>m>>k;
  54. for (int i=;i<n;i++)
  55. for (int j=;j<m;j++)
  56. cin>>map[i][j];
  57. bfs();
  58. cout<<ans<<endl;
  59. }
  60. }

数据结构——UVA 1600 机器人巡逻的更多相关文章

  1. UVa——1600(巡逻机器人)

    迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值.比较简单的三维bfs.写搜索题的话一定要注意细节.这个题花了好长的时间.因为k的原因,一开始用了k的原因 ...

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

    UVA 1600 Patrol Robot   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   ...

  3. UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)

    非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...

  4. UVA - 1600 Patrol Robot (巡逻机器人)(bfs)

    题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...

  5. UVa 1600 Patrol Robot (习题 6-5)

    传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...

  6. UVa 1600 Patrol Robot(三维广搜)

    A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...

  7. UVa 12549 机器人警卫(最小点覆盖)

    https://vjudge.net/problem/UVA-12549 题意: 在一个Y行X列的网格里有空地(.),重要位置(*)和障碍物(#),用最少的机器人看守所有重要位置,每个机器人要放在一个 ...

  8. UVa 1600 Patrol Robot(BFS)

    题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...

  9. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

随机推荐

  1. 关于WINDOWS命令

    1. Windows netstat 查看端口.进程占用 netstat -aon —  显示全部进程 2. 查看进程命令 tasklist — 显示全部进程 taskkill — 关闭至少一个系统进 ...

  2. 巧用hidden传递参数

  3. struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解

    http://www.cnblogs.com/langtianya/archive/2013/04/10/3012205.html

  4. CI 笔记2,(命令规范等)

    调试模式开启,$this->output->enable_profiler(TRUE); 保留字,不能和控制器重名,有3个,CI_Controller ,Default, index.这三 ...

  5. method=“post/get”

    Form表单中method="post/get'的区别   Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据 ...

  6. 【转】IOS 30多个iOS常用动画,带详细注释

    原文: http://blog.csdn.net/zhibudefeng/article/details/8691567 CoreAnimationEffect.h 文件 // CoreAnimati ...

  7. Qt经典出错信息之”Basic XLib functionality test failed!”

    解决方法: 此完整出错信息是在./configure阶段Basic XLib functionality test failed!You might need to modify the includ ...

  8. SGU 131.Hardwood floor

    时间限制:0.25s 空间限制:4M 题意: 给出 n*m (1≤n.m≤9)的方格棋盘,用 1*2 的矩形的骨牌和 L 形的(2*2 的 去掉一个角)骨牌不重叠地覆盖,求覆盖满的方案数. Solut ...

  9. Python下调用json.dumps中文显示问题解决办法

    json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: import json js = json.loads('{"haha": & ...

  10. java中的异常结构

    1.基类为Throwable. 2.Error和Exception分别继承Throwable. 3.Error类异常描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对 ...