UVA1600 Patrol Robot
来源:互联网 发布:古筝大师软件 编辑:程序博客网 时间:2024/05/18 00:04
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 Output
7
10
-10Sample Output710-1
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.
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int nx[4][2]= {0,-1,0,1,-1,0,1,0};int n,m,k,vis[30][30][30],map[30][30];struct node{ int x,y; int s,t; //node(int x=1,int y=1,int s=0,int t=0):x(x),y(y),s(s),t(t) {}};int bfs(){ queue<node>q; node now,tmp,a; a.x=0;a.y=0;a.s=0;a.t=0; q.push(a); vis[0][0][0]=1; while(!q.empty()) { now=q.front(); q.pop(); if(now.x==m-1&&now.y==n-1)return now.s; for(int i=0; i<4; i++) { int xx=now.x+nx[i][0]; int yy=now.y+nx[i][1]; int tt=now.t; if(map[xx][yy]==1) tt++; else tt=0; if(!vis[xx][yy][tt]&&xx>=0&&xx<m&&yy>=0&&yy<n&&tt<=k) { vis[xx][yy][tt]=1; tmp.x=xx; tmp.y=yy; tmp.s=now.s+1; tmp.t=tt; q.push(tmp); } } } return -1;}int main(){ int i,j,T; scanf("%d",&T); while(T--) { memset(vis,0,sizeof(vis)); scanf("%d%d",&m,&n); scanf("%d",&k); for(i=0; i<m; i++) for(j=0; j<n; j++) scanf("%d",&map[i][j]); printf("%d\n",bfs()); } return 0;}
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 Output
7
10
-10Sample Output710-1
阅读全文
1 0
- uva1600 - Patrol Robot
- UVA1600 - Patrol Robot
- uva1600 Patrol Robot
- uva1600 -- Patrol Robot
- UVA1600 Patrol Robot
- Patrol Robot UVa1600巡逻机器人
- Uva1600——Patrol Robot
- Patrol Robot UVa1600巡逻机器人
- uva1600-Patrol Robot(bfs)
- Uva1600 - Patrol Robot(bfs解法)
- 习题6-5 UVa1600 Patrol Robot(BFS)
- uva1600 Patrol Robot 巡逻机器人(BFS)
- Uva1600 Patrol Robot 【bfs】【习题6-5】
- uva1600 Patrol Robot —— bfs
- 算法竞赛入门经典 第二版 习题6-5 巡逻机器人 Patrol Robot uva1600
- UVa1600 习题6-5 巡逻机器人 (Patrol Robot,ACM/ICPC Hanoi 2006)
- 习题6-5 巡逻机器人(Patrol Robot, ACM/ICPC Hanoi 2006, UVa1600)
- 巡逻机器人(Patrol Roboot,UVa1600)
- css3属性总结(中)
- Java 构造函数
- Myeclipse2014的激活工具run.bat闪退的完美解决
- 运算符重载实现判断集合异同
- 设计模式-创建型软件设计模式(二)
- UVA1600 Patrol Robot
- 机器学习:逻辑回归
- 利用命令部署springBoot项目出现ERROR] No plugin found for prefix ‘spring-boot’ in the current的错误
- Vagrant在,win7/win10系统下搭建使用
- RocketMQ原理解析-broker 6.索引服务
- Hadoop,Java,MySQL,Linux,ML,DL,Spark大数据跳槽班视频下载
- strlen strcmp strcat strcpy strncpy strstr strchar函数的实现
- linux下安装nginx详解和与tomcat的使用
- Java环境搭建---JDK+TOMCAT+ECLIPSE下载安装配置