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,
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
题意:
在一个m*n的矩形格子中,要求求出从点(0,0)到点(m,n)的最短步伐。可是途中会设有障碍。所以要用一个三维数字标记起来。
思路:

用一个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[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct Node{
int x,y;
int cnt;
int k;
};
int init(){
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
cin>>n>>m>>k;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
cin>>map[i][j];
}
int bfs(){
queue<Node> q;
Node u;
u.x=0;u.y=0;u.cnt=0;u.k=k;
vis[0][0][k]=1;
q.push(u);
while (!q.empty()){
u=q.front();q.pop();
if (u.x==n-1&&u.y==m-1){
ans=u.cnt;
return 0;
}
Node v;
for (int i=0;i<4;i++){
v.x=u.x+dx[i];
v.y=u.y+dy[i];
v.cnt=u.cnt+1;
if (map[v.x][v.y]) v.k=u.k-1;
else v.k=k;//碰到0就恢复满命
if (v.x>=0&&v.x<n&&v.y>=0&&v.y<m&&!vis[v.x][v.y][v.k]){
if (v.k>=0) {q.push(v);vis[v.x][v.y][v.k]=1;}
}
}
}
if (q.empty()) ans=-1;
}
int main()
{
int T;
cin>>T;
while (T--){
init();
bfs();
cout<<ans<<endl;
}
}

UVA 1600的更多相关文章

  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 机器人巡逻

    描述 A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n colu ...

  4. UVA 1600 Patrol Robot

    带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...

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

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

  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 1600 Patrol Robot (BFS 最短路)

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

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

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

  9. UVa 1600 Patrol Robot(BFS)

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

随机推荐

  1. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  2. squid+nginx+apache

    一.前言 二.编译安装 三.安装MySQL.memcache 四.安装Apache.PHP.eAccelerator.php-memcache 五.安装Squid 六.后记 一.前言,准备工作当前,L ...

  3. MySQL基础之第13章 MySQL函数

    13.1.数学函数 随机数可能会用到,其他基本无视. 13.2.字符串函数 重点CONCAT(S1,S2….) 13.3.日期和时间函数 一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+ ...

  4. [Everyday Mathematics]20150121

    设 $f\in C[0,1]$ 适合 $$\bex xf(y)+yf(x)\leq 1,\quad\forall\ x,y\in [0,1]. \eex$$ 试证: $$\bex \int_0^1 f ...

  5. Android 引用library project

    1.如何将一个android工程作为库工程(library project) library project是作为jar包被其它android工程使用的,首先它也是普通的android工程.然后: 1 ...

  6. ant 安装过程中问题记录

    最近在本机安装ant过程中出现一些问题,在此记录一下. 1.Unable to locate tools.jar. Expected to find it in C:/Program Files/Ja ...

  7. hdu5073 简单枚举+精度处理

    其实这题还是挺简单的,因为移动k个星球后,这k个星球的权值就可以变为0,所以只有剩下的本来就是连着的才是最优解,也就是说要动也是动两端的,那么就O(N)枚举一遍动哪些就好了. 我是在杭电oj题目重现的 ...

  8. 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址

    转载请说明原出处,谢谢~~ 前些日子用wke内核封装了duilib的webkit浏览器控件,好多群里朋友私聊我希望可以我公布源码,今天把这个控件的源码和使用demo公布.其实这个控件封装起来没什么难度 ...

  9. 第2课 讲解主流三大web服务器之Apache服务器(httpd服务器) - 大型网站高并发架构与自动化运维实战(六)

    基本配置 配置固定IP地址 打开默认的网卡配置文件 cd /etc/sysconfig/network-script/ cp ifcfg-eth0 ifcfg-eth1 vim ifcfg-eth0 ...

  10. How to interact with the Chef Server using the Chef Server API using Shell script

    !/usr/bin/env bash   _chef_dir () { # Helper function: # Recursive function that searches for chef c ...