UVA 1600
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.
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(0k
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.
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.
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的更多相关文章
- UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
UVA 1600 Patrol Robot Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
- 数据结构——UVA 1600 机器人巡逻
描述 A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n colu ...
- UVA 1600 Patrol Robot
带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...
- UVa——1600(巡逻机器人)
迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值.比较简单的三维bfs.写搜索题的话一定要注意细节.这个题花了好长的时间.因为k的原因,一开始用了k的原因 ...
- 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 最短路)
这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #inclu ...
- UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)
非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...
- UVa 1600 Patrol Robot(BFS)
题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...
随机推荐
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- erl0001-Erlang 设计原则 process port io
Erlang原理 (转载自ITEYE cryolite博客 ps:精彩)by Robert Virding This is a description of some of the basic pro ...
- ActionBarSherlock的学习笔记(一) -------------- ActionBarSherlock的简要介绍
1. 介绍 ActionBarSherlock 是Android compatibility library 的一个扩展, 不知道什么原因 Android 兼容开发包没有包含ActionBar. 所以 ...
- 【Ajax】实现注册页面判断用户名是否可用的提示—异步加载
效果如图 在注册或登录网站时,当我们输入错误格式的账号或信息时,会看到这种提示. 那怎么实现呢,通过ajax异步加载的方式,可以实现不刷新页面就显示出该提示. 实现 首先创建一个JSP,写一个简单的页 ...
- mysql优化小技巧
对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索引.主键索引.唯一索引unique.全文索引] c: 分表技术(水平 ...
- Oracle行列互换 横表和纵表
/* 在实际使用sql工作中总会碰到将某一列的值放到标题中显示.就是总说的行列转换或者互换. 比如有如下数据: ID NAME KECHENG CHENGJI -- ---------- ------ ...
- Microsoft-pubs(图书馆管理系统)-数据库设计
ylbtech-DatabaseDesgin:微软提供-pubs(图书馆管理系统)-数据库设计 1.A,数据库关系图 1.B,数据库设计脚本 -- ======================== ...
- ISAPI在IIS7上的配置
主要介绍ISAPI的作用.ISAPI在IIS7上的配置.开发ISAPI的基本内容及使用VS 2008配置ISAPI DLL开发项目. 一.ISAPI介绍 缩写词=Internet Server App ...
- 学习笔记——XSLT转换器的使用(Xalan和Saxon) .(转)
转自:http://blog.csdn.net/crystalbruce/article/details/7401602 XSLT分为两类: 1:客户端转换:需要浏览器的支持. 2:服务器转换:需要使 ...
- codevs1796-最小完全图
表示第一篇就是水题. 根据Prim的思想,我们可以证明:dis ( a , b ) > max { a b 最小生成树路径上的边权 } 把所有边sort一遍用并查集维护就可以了 #include ...