数据结构——UVA 1600 机器人巡逻
描述

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
***解题思路:
用一个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 机器人巡逻的更多相关文章
- UVa——1600(巡逻机器人)
迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值.比较简单的三维bfs.写搜索题的话一定要注意细节.这个题花了好长的时间.因为k的原因,一开始用了k的原因 ...
- UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
UVA 1600 Patrol Robot Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)
非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...
- UVA - 1600 Patrol Robot (巡逻机器人)(bfs)
题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
- 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 12549 机器人警卫(最小点覆盖)
https://vjudge.net/problem/UVA-12549 题意: 在一个Y行X列的网格里有空地(.),重要位置(*)和障碍物(#),用最少的机器人看守所有重要位置,每个机器人要放在一个 ...
- UVa 1600 Patrol Robot(BFS)
题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
随机推荐
- List(列表)
List(列表): List的特征是其元素以线性方式存储,集合中可以存放重复对象. List接口主要实现类包括: ArrayList() : 代表长度可以改变得数组.可以对元素进行随机的访问,向Arr ...
- 试着开发chrome插件
我的第一个chrome插件,是app形式的 代码如下 创建一个文件: 1.manifest.json { "version": "1.0", "man ...
- XML有哪些解析方式有何优缺点?xml有哪些解析技术?区别是什么?
有DOM,SAX,STAX等 (1):DOM:处理大型文件时其性能下降的非常厉害.这个问题是由DOM的树结构所造成的,这种结构占用的内存较多,而且DOM必须在解析文件之前把整个文档装入内存,适合对XM ...
- 【HDU3802】【降幂大法+矩阵加速+特征方程】Ipad,IPhone
Problem Description In ACM_DIY, there is one master called “Lost”. As we know he is a “-2Dai”, which ...
- css3基础教程十六变形与动画animation
前面我们讲过的变形与动画一般都是通过鼠标的单击.获得焦点,被点击或对元素进行一定改变后以后触发效果的,那么有没有像Flash一样自动播放的动画效果呢?答案当然是肯定的,这就是我们今天要讲到的anima ...
- IoC模式(控制反转)(转)
转自:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html,写的很好,用C#代码解释控制反转,然后更进一步,提到依赖注入是控制反转的 ...
- SQL UNION 和 UNION ALL 操作符
SQL UNION 和 UNION ALL 操作符 SQL Full Join SQL Select Into SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结 ...
- 18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...
- dede定义全局变量(include/common.inc.php)及调用方式
dede定义全局变量的文件include/common.inc.php及使用 在include/common.inc.php文件里,dede定义了大量的全局变量,详细自己去看看 dede模板里 ...
- php 钩子函数原理 解析
目前对钩子的理解:<转载:http://www.cnblogs.com/del/archive/2008/02/25/1080825.html> 譬如我们用鼠标在某个窗口上双击了一次, 或 ...