A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (ij)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 (xy) to (x + 1, y), (xy + 1), (x - 1, y) or (xy - 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 (mn). 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(1≤mn≤20). The second line contains an integer number k (0≤k≤20). 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 ofaij is 1 if there is an obstacle on the cell (ij), and is 0 otherwise.

Output

For each data set, if there exists a way for the robot to reach the cell (mn), 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

Simple Output

7
10
-1

题意

机器人从起点(0,0)走到(m,n),图中有障碍,机器人最多连续翻越k个障碍,求走到(m,n)的最短路径长度

题解1

一开始在二维上想怎么连续,而且已经访问过的点有可能再访问(本来的连续大),最后硬着头皮想了个蠢方法(if去判断)

代码1

 #include<bits/stdc++.h>
using namespace std;
int Map[][],Cnt[][];//Map的值为连续越过的数量,Cnt的值为步数
int dx[]={,,,-};
int dy[]={,-,,};
int n,m,k;
struct Node
{
int x,y;
Node(int x=,int y=):x(x),y(y){}
};
int bfs()
{
queue<Node> qu;
Cnt[][]=;
qu.push(Node(,));
Node h,t;
while(!qu.empty())
{
h=qu.front();
qu.pop();
if(h.x==n&&h.y==m)return Cnt[n][m];
for(int i=;i<;i++)
{
t.x=h.x+dx[i];
t.y=h.y+dy[i];
if(t.x>=&&t.x<=n&&t.y>=&&t.y<=m)
{
//h表示当前,t表示下一个
if(Map[t.x][t.y]!=&&Map[h.x][h.y]!=)//当前和下一个!=0表示连续
if(Cnt[t.x][t.y]==-)//未访问过
Map[t.x][t.y]=Map[h.x][h.y]+;//下一个连续=当前连续+1
else if(Map[t.x][t.y]>Map[h.x][h.y]+)//若已访问过,如果下一个已经有的连续>当前+1
{
Map[t.x][t.y]=Map[h.x][h.y]+;//保证当前连续为最小连续,为了下一步走得更舒服
Cnt[t.x][t.y]=-;//标记未访问
}
if(Map[t.x][t.y]!=&&Map[h.x][h.y]==)//表示这是第一个连续
if(Cnt[t.x][t.y]==-)//未访问过
Map[t.x][t.y]=;//这个点为初始连续1
else if(Map[t.x][t.y]>)//若已访问过,如果下一个连续不是初始连续1,设为初始连续1
{
Map[t.x][t.y]=;//保证当前连续为最小连续,为了下一步走得更舒服
Cnt[t.x][t.y]=-;//标记未访问
}
if(Map[t.x][t.y]<=k&&Cnt[t.x][t.y]==-)//连续<=k,未访问
{
Cnt[t.x][t.y]=Cnt[h.x][h.y]+;
qu.push(t);
}
//上面处理的很蠢
}
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
memset(Cnt,-,sizeof(Cnt));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&Map[i][j]);
printf("%d\n",bfs());
}
return ;
}

题解2

后来发现可以用三维广搜去写,增加的第三维表示到这个点[X,Y]步数为STEP的情况

代码2

 #include<bits/stdc++.h>
using namespace std;
int Map[][],Cnt[][],Vis[][][];//Vis[x][y][step]
int dx[]={,,,-};
int dy[]={,-,,};
int n,m,k;
struct Node
{
int x,y,step;//step为连续数
Node(int x=,int y=,int step=):x(x),y(y),step(step){}
};
int bfs()
{
queue<Node> qu;
Cnt[][]=;
Vis[][][]=;
qu.push(Node(,,));
Node h,t;
while(!qu.empty())
{
h=qu.front();qu.pop();
if(h.x==n&&h.y==m)return Cnt[n][m];
for(int i=;i<;i++)
{
t.x=h.x+dx[i];
t.y=h.y+dy[i];
t.step=h.step;
if(t.x>=&&t.x<=n&&t.y>=&&t.y<=m)
{
if(Map[t.x][t.y]==)t.step=h.step+;//墙,连续+1
else t.step=;//不是墙,连续=0
if(t.step<=k&&Vis[t.x][t.y][t.step]==)//连续<=k并且未访问
{
Vis[t.x][t.y][t.step]=;//标记访问
if(Cnt[t.x][t.y]==-)//未走到过(第一次走到的肯定是最小路径)
Cnt[t.x][t.y]=Cnt[h.x][h.y]+;
qu.push(t);
}
}
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
memset(Cnt,-,sizeof(Cnt));
memset(Vis,,sizeof(Vis));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&Map[i][j]);
printf("%d\n",bfs());
}
return ;
}

UVa 1600 Patrol Robot(三维广搜)的更多相关文章

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

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

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

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

  5. UVA 1600 Patrol Robot

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

  6. UVa 1600 Patrol Robot(BFS)

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

  7. UVa 1600 Patrol Robot【BFS】

    题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...

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

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

  9. PAT L3-004 肿瘤诊断(三维广搜)

    在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每张切片的尺寸(即每张切片 ...

随机推荐

  1. DLL 调试(C# 调用 C++ 的 DLL)

    操作步骤: (1) C++ 的 DLL 项目中设置断点: (2) C# 工程右键[属性]->[调试]->[启动调试器]中选中[启动本机代码调试]. 注:要调试 DLL 必须有 DLL 的源 ...

  2. Vue.js学习和第一个实例

    第一个实例效果图: 1.node.js下载,然后安装.下载地址:链接:http://pan.baidu.com/s/1o7TONhS 密码:fosa 2.下载Vue.js.链接:http://pan. ...

  3. ABAP-折叠窗口

    1.测试 2.代码 *&---------------------------------------------------------------------* *& Report ...

  4. jquery 设计的扩展---初级

    1. 写一个构造函数G,调用G 时,返回G上的fn 对象的init() 的实例 2.设置G.fn 的指向,使用G.fn 与G.prototype指向同一个对象 2.1 重写G.prototype 对象 ...

  5. setitimer函数

    和alarm函数类似,都用于定时操作: 函数原型:int setitimer(int which, const struct itimerval *new_value, struct itimerva ...

  6. 2018.3,GC可控了

    如题,不再像以前那样由系统决定什么时候进行GC,现在可以通过设置,决定自己手动回收还是使用传统的系统决定方式. 传统方式不可控,就算是手动调用了GC.COLLECT,系统也不一定会立即执行.

  7. zstack使用笔记之端口转发

    根据官方教程,使用端口转发,可以把公网流量转发到内网 实验环境是这样的 公网网段:172.17.3.x 内网网段:192.168.1.x 首先创建云路由网络,这个就不说了,根据教程大家都可以创建出来, ...

  8. VUE 计算属性 vs 侦听属性

    计算属性 vs 侦听属性 Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性.当你有一些数据需要随着其它数据变动而变动时,你很容易滥用 watch——特别是如果你之前使用过 ...

  9. 如何杀死oracle死锁进程

    方法一:Oracle的死锁非常令人头疼,总结了一些点滴经验作为学习笔记 1.查哪个过程被锁查V$DB_OBJECT_CACHE视图: '; 2. 查是哪一个SID,通过SID可知道是哪个SESSION ...

  10. 浅析SQL Server 中的SOS_SCHEDULER_YIELD类型的等待

    本文出处:http://www.cnblogs.com/wy123/p/6856802.html 进程的状态转换 在说明SOS_SCHEDULER_YIELD等待之前,先简要介绍一下进程的状态(迷迷糊 ...