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. subprocess.Popen模块

    该类用于在一个新的进程中执行一个子程序.subprocess模块底层的进程创建和管理是由Popen类来处理的. 1.subprocess.Popen的构造函数 class subprocess.Pop ...

  2. Spring cloud info信息显示

    父工程添加配置如下 <build> <finalName>microservicecloud</finalName> <resources> <r ...

  3. APP-7-百度地图移动轨迹

    1.代码部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  4. XML中的变量传值

    在action的java类中定义变量之后,在XML中获取该变量进行对应传值:: 在指定方法中获取XML配置文件的变量传值::

  5. 【Noip模拟 20161004】局域网

    问题描述 所有SZSZ 学生翘首以盼的新教学楼总算快要竣工了,接下来到了网络布线的时候.网络系统的总布局是由nn台计算机组成的有线局域网,每根网线长度为dd,正常情况下,网线是可以缠绕使其变短但是不能 ...

  6. Hive安装与配置--- 基于MySQL元数据

    hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成本低,可以通过 ...

  7. adb push和adb install区别

    一般的,Android 应用程序有两种安装方法: 1. 将应用程序的apk文件push到手机中,用如下命令: adb push xxxx.apk /system/app. 2. 用adb instal ...

  8. centos FTP 用户指定目录禁用上级目录

    在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录,这样会对系统带来安全隐患.可以通过以下三条配置文件来控制用户切换目录.chroot_list_enable=YES/NO(NO)设置是 ...

  9. AT&T汇编格式

    一.汇编语言 (1) 指令 伪指令 (2)常量   表达式[常量 寄存器 标号 变量] 二. AT&T 与 Intel 格式相比应注意的地方 1.寄存器引用 mov %eax,%ebx 2.操 ...

  10. cgi调用linux系统命令

    1.例如:建一个目录:system("mkdir yourdir").(1)首先:要用root用户如果cgi中要用root用户,则必须在boa.conf文件中配置,将User no ...