UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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, n
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 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 题解:求机器人走最短路线,而且可以穿越障碍。
一道典型的BFS遍历的题,用队列实现,找到满足条件的最短路线;
题意很好理解,实现时应该注意判断规定障碍个数和标记走过的点。
BFS比DFS要快很多,一个用队列实现,一个用递归实现,都可以用
BFS的AC代码
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=;
int n,m,k;
int a[maxn][maxn][maxn];
int b[maxn][maxn];
int dir[][]= {-,,,,,-,,};
struct node
{
int x,y,step,zhangai;
void boom(int nx,int ny,int nstep,int nzhangai)
{
x=nx;
y=ny;
step=nstep;
zhangai=nzhangai;
}
}; int bfs()
{
queue<node>q;
while(!q.empty())
q.pop();
node u,v;
u.boom(,,,); //起点
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=; i<; i++)
{
int xx=u.x;
int yy=u.y;
int sstep=u.step;
int szhangai=u.zhangai;
xx+=dir[i][];
yy+=dir[i][];
sstep+=;
if(xx>n||xx<||yy>m||yy<)
continue;
if(b[xx][yy]==)
szhangai++;
else
szhangai=;
if(szhangai>k)
continue;
if(a[xx][yy][szhangai]==) //如果没有这一步会超时
continue;
if(xx==n&&yy==m)
return sstep;
v.boom(xx,yy,sstep,szhangai);
q.push(v);
a[xx][yy][szhangai]=;
}
}
return -;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&m,&k);
memset(a,,sizeof(a)); //每次都要
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&b[i][j]);
int total=bfs();
printf("%d\n",total);
}
return ;
}
下面的是在网上看的一个用dfs实现的,也很容易懂
#include<iostream>
#include<cstring>
using namespace std;
int n,m,k;
int b[][];
int a[][][];
int total;
int xx[]= {-,,,};
int yy[]= {,,,-}; void dfs(int x,int y,int step,int zhangai)
{
if(x == n - && y == m - )
{
total = min(total,step);
return ;
}
for(int i = ; i < ; i++)
{
int nx = x + xx[i];
int ny = y + yy[i];
int st = zhangai;
if(b[nx][ny] == ) st++;
else
st = ;
if(nx >= && nx < n && ny >= && ny < m)
{
if((a[nx][ny][st] < || a[nx][ny][st] > step + ) && st <= k)
{
a[nx][ny][st] = step + ;
dfs(nx,ny,step + ,st);
}
}
}
return ;
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(a,-,sizeof(a));
total = <<;
cin>>n>>m>>k;
for(int i = ; i < n ; i++)
for(int j = ; j < m ; j ++)
cin>>b[i][j];
dfs(,,,);
if(total != << )
cout<<total<<endl;
else
cout<<"-1"<<endl;
}
return ;
}
UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)的更多相关文章
- UVA - 1600 Patrol Robot (巡逻机器人)(bfs)
题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...
- 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 ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
- UVa 1600 Patrol Robot(BFS)
题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...
- UVA 1600 Patrol Robot
带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...
- UVa 1600 Patrol Robot【BFS】
题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...
- Uva 1600 Patrol Robot (BFS 最短路)
这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #inclu ...
- 【UVa】1600 Patrol Robot(dfs)
题目 题目 分析 bfs可以搞,但是我还是喜欢dfs,要记忆化不然会T 代码 #include <cstdio> #include <cstring> #inc ...
随机推荐
- uoj#67. 新年的毒瘤(割顶)
#67. 新年的毒瘤 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树. 这个长着毒瘤的树可以用n个结点m 条无向边的无向图表示.这个图中有一些结点被称作是毒瘤结点,即删掉这个结 ...
- 付出半个小时的笔误级BUG
一开始,我为了偷懒将所有的任务全都压在了一个浮动指针上: for (; CCPtr->S != NULL; CCPtr->S = CCPtr->S->next) // for ...
- linux环境下deb格式 转换成rpm格式
linux环境下deb格式 转换成rpm格式 使用alien工具转换deb格式到rpm格式 alien_8.87.tar.gz 下载alien_8.87.tar.gz [root@mysqlnode2 ...
- 字符编码 ASCII,Unicode 和 UTF-8 概念扫盲
今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步搞清楚. 下面就是我的笔记,主要用来整理自己的思 ...
- 如何定制Windows系统右键菜单
今天心血来潮把几个自己常用的工具定制到了系统的右键菜单.包括notepad++,7zip,还有复制文件全路径和文件夹路径.下面简单介绍一下步骤. 1. Windows系统右键菜单对应的注册表位置 Wi ...
- 当在类中的 Parcelable 接口使用 ArrayList < customObject > android 系统: nullPointerException
我想使调用音乐使用 parcelable,所以我可以访问两个不同的活动中的音乐的实例的类.我不想使用可序列化的速度的目的.当我尝试将它使用传递时,我总是收到 nullPointerException: ...
- 辛星浅析跨域传输的CORS解决方式
首先我们有一个概念.那就是"同源准则",也就是same-origin policy,它要求一个站点(协议+主机+port号)来确定的脚本.XMLHttpRequest和Webso ...
- CentOS 安装 Tomcat
1.Tomcat官网获(http://tomcat.apache.org/)取tar.gz文件的下载地址 2.下载: # wget http://apache.fayea.com/tomcat/tom ...
- VS项目属性配置实验过程
(原创,转载注明出处:http://www.cnblogs.com/binxindoudou/p/4017975.html ) 一.实验背景 cocos2d-x已经发展的相对完善了,从项目的创建.编译 ...
- migrate from weblogic to tomcat: directory mapping--reference
Question: I am trying to migrate from weblogic to tomcat. in weblogic I have <virtual-directory-m ...