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 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

3
2 5
0
0 1 0 0 0
0 0 0 1 0
4 6
1
0 1 1 0 0 0
0 0 1 0 1 1
0 1 1 1 1 0
0 1 1 1 0 0
2 2
0
0 1
1 0

Sample Output

7
10
-1
题意:
在一个m*n的矩形格子中,要求求出从点(0,0)到点(m,n)的最短步伐。可是途中会设有障碍。所以要用一个三维数字标记起来。
思路:

用一个vis[x][y][z]表示走到x,y的时候 穿过了z个墙,如今的步数是什么

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

代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 20+5
using namespace std;
int map[maxn][maxn],vis[maxn][maxn][maxn];
int m,n,k,ans;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct Node{
int x,y;
int cnt;
int k;
};
int init(){
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
cin>>n>>m>>k;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
cin>>map[i][j];
}
int bfs(){
queue<Node> q;
Node u;
u.x=0;u.y=0;u.cnt=0;u.k=k;
vis[0][0][k]=1;
q.push(u);
while (!q.empty()){
u=q.front();q.pop();
if (u.x==n-1&&u.y==m-1){
ans=u.cnt;
return 0;
}
Node v;
for (int i=0;i<4;i++){
v.x=u.x+dx[i];
v.y=u.y+dy[i];
v.cnt=u.cnt+1;
if (map[v.x][v.y]) v.k=u.k-1;
else v.k=k;//碰到0就恢复满命
if (v.x>=0&&v.x<n&&v.y>=0&&v.y<m&&!vis[v.x][v.y][v.k]){
if (v.k>=0) {q.push(v);vis[v.x][v.y][v.k]=1;}
}
}
}
if (q.empty()) ans=-1;
}
int main()
{
int T;
cin>>T;
while (T--){
init();
bfs();
cout<<ans<<endl;
}
}

UVA 1600的更多相关文章

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

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

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

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

  3. 数据结构——UVA 1600 机器人巡逻

    描述 A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n colu ...

  4. UVA 1600 Patrol Robot

    带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...

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

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

  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 1600 Patrol Robot (BFS 最短路)

    这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #inclu ...

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

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

  9. UVa 1600 Patrol Robot(BFS)

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

随机推荐

  1. Android手动画柱状图的例子

    效果图如上,网上看到的例子,谨以此文记录一下,以后用到的地方再来翻翻. 核心技术是用Canvas和Paint画长方形. 源码地址:http://download.csdn.net/detail/abc ...

  2. andorid 文字颜色selector的使用

    文字颜色selector的使用 创建名称为selector_1的Android项目,在res/color文件夹下创建名称为button_selector_text.xml的文字颜色配置文件,代码如下 ...

  3. Linux系统性能监控

    系统的性能指标主要包括CPU.内存.磁盘I/O.网络几个方面. 1. CPU性能 (1)利用vmstat命令监控系统CPU 该命令可以显示关于系统各种资源之间相关性能的简要信息,这里我们主要用它来看C ...

  4. ORA-15018: diskgroup cannot be created

    创建ASM磁盘组的时候出错,具体报错如下: SQL> create diskgroup kel external redundancy disk 'ORCL:KEL1','ORCL:KEL2'; ...

  5. 【和我一起学python吧】初学Python,版本如何选择?

    早在四年多以前,在我进入英才网之前,去面试过一家海归创业的公司.他们需要的是有unix开发经验的技术人员,但是因为他们当时所处的阶段对很多成熟 技术人员不是很吸引,所以条件放宽为熟悉面向对象的程序开发 ...

  6. JQuery笔记:JQuery和JavaScript的联系与区别

    来源:http://www.ido321.com/1019.html ps:LZ觉得这个标题有点大了,超出了能力范围,不喜勿碰.目前只记录LZ能力范围内的,日后持续补充. 一.JQuery对象和DOM ...

  7. ExcelUtils 导表实例

    @RequestMapping("/dealer/chargebook/exportv.htm")    public void getChargeBookList(int epm ...

  8. 3669 [Noi2014]魔法森林(LCT,最小生成树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3669 [题意] 给定一个无向图,求1-n的路径中最小的max{ai}+max{bi} ...

  9. android string[] arraylist<string>互转

    string[] to arraylist<string>:eg:string[] str; arraylist<string> arr; for(int i=0;i<s ...

  10. mvn deploy 报错:Return code is: 400, ReasonPhrase: Bad Request. ->

    mvn deploy 报错:Return code is: 400, ReasonPhrase: Bad Request. -> TEST通过没有报错,但是最终部署到Nexus中时出现错误. 后 ...