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. vue 设置背景

    <span :style="{ 'background': 'url(' + aboutImg1 + ') no-repeat center center', 'background- ...

  2. js判断对象

    一般学java的小伙伴,刚开始写js时如果遇到要判断一个字符串是否不为空,往往会这样写 if(str != undefined && str != null && st ...

  3. UNITY2018开启deepprofiling

    ADB方式调试游戏步骤 前提: 1,手机开启 [开发者模式][USB调试] 2,数据线连接手机和电脑 3,安装adb(注意adb版本不对可能导致adb deveices找不到设备,那就换个adb版本) ...

  4. NGUI 背景图自适应

    背景图UISprite组件调整如下: UIRoot设置: 不保持比例自适应: 保持宽与屏幕宽一致,高度随宽的缩放比例进行缩放:

  5. 剑指offer例题——旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  6. C++中 int i 与 int &i 注意事项

    来源:http://blog.csdn.net/qianchenglenger/article/details/16949689 1.int i 传值,int & i 传引用 int i不会回 ...

  7. jenkins com.jcraft.jsch.JSchException: Auth cancel

    jenkins构建时报如下错误: 首先去系统管理--->系统设置上看看SCP插件中的用户名和密码是否正确

  8. 14.Java集合简述.md

    Java的集合类别,分为两类Collection和Map,Collenction包含了Set: •Set:无序,不可重复的集合 •List:有序,重复的集合 •Map:具有映射关系的集合 •Queue ...

  9. Celery 图,[转]

    https://www.cnblogs.com/forward-wang/p/5970806.html

  10. python字典dict的成对运算

    dict = {'age': 18, 'name': 'jin', 'sex': 'male', }# for k,v in dict.items():# print(k,v)# v1 = dict[ ...