Problem UVA1600-Patrol Robot

Accept:529  Submit:4330

Time Limit: 3000 mSec

Problem Description

A robot has to patrol around a rectangular area which is in a form of m × n 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 (1 ≤ m,n ≤ 20). The second line contains an integer number k (0 ≤ k ≤ 20). The i-th 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 Ouput

7

10

-1

题解:自己做的第一道可以穿过障碍物的搜索,原来做的都相当于k==0的情况,这个题里多了一个可以穿越障碍物,但不能连续穿越多个的限制,这时再用一般的套路是会出错的。做过八数码的同学应该有感觉,BFS搜索中,判重是一件至关重要的事情,它可以避免大量无谓的搜索以及神奇的死循环。这里如果把判重数组定义成二维vis,就只能判断是否到过这里,而忽略了还能穿越几个障碍物这一参数。很有可能从两条路过来,长度相同但是其中一条还可以穿越更多的障碍,还有可能虽然暂时性的其中一条路到这里的时间戳较早,但是它能够继续穿越的障碍物也较少,对最终结果来说不如一条时间戳稍晚,但是还能穿越很多障碍物的路线,这些情况会被二维vis提前堵死,是很不可取的。解决方案就是vis数组加一维表示穿越了几个障碍物到达这里,这样再去判重就没问题了。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; struct Point{
int x,y,time;
int layer;
Point(int x = ,int y = ,int time = ,int layer = ) :
x(x),y(y),time(time),layer(layer) {}
}; const int maxn = ;
int gra[maxn][maxn];
int vis[maxn][maxn][maxn];
int n,m,k;
int dx[] = {,-,,};
int dy[] = {,,-,}; int bfs(){
queue<Point> que;
que.push(Point(,,,));
memset(vis,,sizeof(vis));
vis[][][] = ;
while(!que.empty()){
Point first = que.front();que.pop();
if(first.x==n && first.y==m) return first.time;
int xx,yy;
for(int i = ;i < ;i++){
xx = first.x+dx[i],yy = first.y+dy[i];
if(<=xx && <=yy && xx<=n && yy<=m){
int layer = first.layer;
if(gra[xx][yy]) layer++;
else layer = ;
if(layer<=k && !vis[xx][yy][layer]){
vis[xx][yy][layer] = ;
que.push(Point(xx,yy,first.time+,layer));
}
}
}
}
return -;
} int main()
{
//freopen("input.txt","r",stdin);
int iCase;
scanf("%d",&iCase);
while(iCase--){
scanf("%d%d",&n,&m);
scanf("%d",&k);
for(int i = ;i <= n;i++){
for(int j = ;j <= m;j++){
scanf("%d",&gra[i][j]);
}
}
printf("%d\n",bfs());
}
return ;
}

UVA1600-Patrol Robot(BFS进阶)的更多相关文章

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

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

  2. UVA1600 Patrol Robot

    题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...

  3. Uva 1600 Patrol Robot (BFS 最短路)

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

  4. UVa 1600 Patrol Robot(BFS)

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

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

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

  6. UVa 1600 Patrol Robot【BFS】

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

  7. 【习题 6-5 UVA-1600】Patrol Robot

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设dis[x][y][z]表示到(x,y)连续走了z个墙的最短路 bfs一下就ok [代码] /* 1.Shoud it use l ...

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

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

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

随机推荐

  1. css中的float属性以及清除方法 (2011-09-03 17:36:26)

    CSS里面的浮动属性是布局的常用工具,只有真正了解它并熟练使用才能将它的优点发挥到极致. 许多页面中都有文字绕图效果,并且各区块分布得错落有置,很多朋友在自学CSS布局时为了做出这些效果往往会被div ...

  2. osgi.net框架简介

    osgi.net是一个动态的模块化框架.它向用户提供了模块化与插件化.面向服务构架和模块扩展支持等功能.该平台是OSGi联盟定义的服务平台规范移植到.NET的实现. 简介 尤埃开放服务平台是一个基于. ...

  3. js 提交数组到后端(C#)

    JS 代码: <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script> // ...

  4. c# 对Url 解码编码

    /// <summary> /// 对Url进行编码 /// </summary> /// <param name="url">url</ ...

  5. R0~R16寄存器作用

    R0-R3     用作传入函数参数,传出函数返回值.在子程序调用之间,可以将 r0-r3 用于任何用途. 被调用函数在返回之前不必恢复 r0-r3.如果调用函数需要再次使用 r0-r3 的内容,则它 ...

  6. 【操作系统】二、JVM线程与Linux内核线程的映射

    Linux从内核2.6开始使用NPTL (Native POSIX Thread Library)支持,但这时线程本质上还轻量级进程. Java里的线程是由JVM来管理的,它如何对应到操作系统的线程是 ...

  7. CDN使用心得:加速双刃剑

    文章图片存储在GitHub,网速不佳的朋友,请看<CDN 使用心得:加速双刃剑> 或者 来我的技术小站 godbmw.com 本文以腾讯云平台的 CDN 服务为例,记录下在个人网站开发和公 ...

  8. deepin使用笔记-解决蓝牙设备开机自动开启的问题

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 1.安装蓝牙驱动管理 #apt-get install blueman 2.打开蓝牙驱动管理,关闭设备 3.关闭蓝牙开机启动服务 ...

  9. canvas纯绘制雨伞、飞机、五角星、桃心,无逻辑

    由于网上很多都是用很多算法和逻辑使用canvas进行绘制,但有时也无法解决一些小众需求 . 为了满足需求不能写运算纯手写,感觉真的很浪费时间,只有自己踩过的坑,才不想看到别人也被坑.我很懒,也想过弄个 ...

  10. 网络基础 cookie详解

    cookie详解 by:授客 QQ:1033553122 cookie干嘛用的? 参见文章http 会话(session)详解: 网络基础 http 会话(session)详解   cookie分类 ...