描述

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, n20). The second line contains an integer number k(0k20). 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

***解题思路:

用一个vis[x][y][z]表示走到x,y的时候 穿过了z个墙,标记现在的步数

进行递归的条件是,走到下一步时候,之前走到这里的步数必须下与之后走到这里的步数。

程序代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 20+5
using namespace std;
int map[maxn][maxn],vis[maxn][maxn][maxn];
int m,n,k,ans;
int dx[]={,,,-};
int dy[]={,-,,};
struct Node{
int x,y;
int cnt;
int k;
}; int bfs(){
queue<Node> q;
Node u;
u.x=;u.y=;u.cnt=;u.k=k;
vis[][][k]=;
q.push(u);
while (!q.empty()){
u=q.front();q.pop();
if (u.x==n-&&u.y==m-){
ans=u.cnt;
return ;
}
Node v;
if (u.k>=)//如果到该点没有命了, 那么就不需要在该点扩展了
for (int i=;i<;i++){
v.x=u.x+dx[i];
v.y=u.y+dy[i];
v.cnt=u.cnt+;
if (map[v.x][v.y]) v.k=u.k-;
else v.k=k;//碰到0就恢复满命
if (v.x>=&&v.x<n&&v.y>=&&v.y<m&&!vis[v.x][v.y][v.k]){
if (v.k>=) {q.push(v);vis[v.x][v.y][v.k]=;}
}
}
}
if (q.empty()) ans=-;
}
int main()
{
int T;
cin>>T;
while (T--){
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
cin>>n>>m>>k;
for (int i=;i<n;i++)
for (int j=;j<m;j++)
cin>>map[i][j];
bfs();
cout<<ans<<endl;
}
}

数据结构——UVA 1600 机器人巡逻的更多相关文章

  1. UVa——1600(巡逻机器人)

    迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值.比较简单的三维bfs.写搜索题的话一定要注意细节.这个题花了好长的时间.因为k的原因,一开始用了k的原因 ...

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

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

  3. UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)

    非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...

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

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

  5. UVa 1600 Patrol Robot (习题 6-5)

    传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...

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

  7. UVa 12549 机器人警卫(最小点覆盖)

    https://vjudge.net/problem/UVA-12549 题意: 在一个Y行X列的网格里有空地(.),重要位置(*)和障碍物(#),用最少的机器人看守所有重要位置,每个机器人要放在一个 ...

  8. UVa 1600 Patrol Robot(BFS)

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

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

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

随机推荐

  1. asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  2. Android开发手记(31) 使用MediaRecorder录音

    使用Android手机的时候,有时我们会用到录音功能,本文简单的介绍了如何使用MediaRecorder通过手机自带麦克风进行录音. 首先,既然是录音,我们需要录音和写外存的权限: <uses- ...

  3. 转-SecureCRT设置

    原帖地址:http://www.2cto.com/os/201410/341569.html 一.基本设置 1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1 ...

  4. Xcode的控制台调试命令

    XCode4.0以后,编译器换成了LLVM 编译器 2.0 与以前相比,更加强大:1.LLVM 编译器是下一带开源的编译技术.完全支持C, Objective-C, 和 C++.2.LLVM 速度比 ...

  5. (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  6. C++序列化库的实现

    C++中经常需要用到序列化与反序列化功能,由于C++标准中没有提供此功能,于是就出现了各式各样的序列化库,如boost中的,如谷歌的开源项目,但是很多库都依赖其他库过于严重,导致库变得很庞大.今天来分 ...

  7. javascript——集合类

    /** * Created by Administrator on 2015/4/14. */ function Set() { this.values = {}; this.n = 0; this. ...

  8. mysql锁死的现象判断

    一般发生表锁死这种低级问题,就有两种情况:1.程序员水平太菜,2.程序逻辑错误. 一旦发生系统会出现超时,关键是有可能你看不到正在活动的php进程,而系统的慢查询日志也不会记录,只能通过show fu ...

  9. linux如何开机以命令行形式启动?

    在管理员权限下,修改/etc/inittab文件即可.把id:5:initdefault:改为id:3:initdefault:就可以了. 如下图所示: 图1: . 图2:

  10. 关于Function()函数对象的那些小九九

    概念:首先,函数是一种特殊类型的数据,函数也是数据类型的一种,实际上函数也是一种对象,函数对象的内建构造器是Function(); 函数的几种创建方式: 函数声明法: function sum(a,b ...