UVA - 1600 Patrol Robot
2015-10-16 19:17
274 查看
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.
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 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.
the number of moves the robot has to make; -1 otherwise.
分析:这题是典型的BFS迷宫最短路问题,但是有个坑点,注意一句话:Therefore, the robot cannot move continuously to more than k cells
containing obstacles. 意思是不能连续移动超过k个障碍格子,移动是可选择的,我开始以为只能一次性移动,要么穿过所有障碍要么过不去。 明白这一点后就很好做了,只需要加一个状态量即可。
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 (1
m, 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 isthe 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
分析:这题是典型的BFS迷宫最短路问题,但是有个坑点,注意一句话:Therefore, the robot cannot move continuously to more than k cells
containing obstacles. 意思是不能连续移动超过k个障碍格子,移动是可选择的,我开始以为只能一次性移动,要么穿过所有障碍要么过不去。 明白这一点后就很好做了,只需要加一个状态量即可。
#include <cstdio> #include <queue> #include <cstring> using namespace std; struct Node { int r, c, k; Node(int r=0, int c=0, int k=0):r(r),c(c),k(k) {} }; const int maxn = 23; const int dr[] = { 0, 1, 0, -1}; const int dc[] = { 1, 0, -1, 0}; int G[maxn][maxn]; int D[maxn][maxn][maxn]; int m, n, k; bool inside(const Node& v) { return v.r>=1&&v.r<=m && v.c>=1&&v.c<=n; } int solve() { memset(D, -1, sizeof(D)); D[1][1][k] = 0; queue<Node> q; q.push(Node(1,1,k)); while(!q.empty()) { Node u = q.front(); q.pop(); if(u.r==m && u.c==n) return D[m] [k]; for(int i=0; i<4; i++) { Node v = u; v.r += dr[i]; v.c += dc[i]; if(G[v.r][v.c]) v.k--; else v.k = k; if(inside(v) && v.k>=0 && D[v.r][v.c][v.k]<0) { D[v.r][v.c][v.k] = D[u.r][u.c][u.k] + 1; q.push(v); } } } return -1; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&m,&n,&k); for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) { scanf("%d",&G[i][j]); } } printf("%d\n",solve()); } return 0; }
相关文章推荐
- java练习题——类与对象
- 消息中间件
- OC 通讯录 中等难度
- 检查死锁的问题
- 第十二周第五天
- 代码重构
- 初试web APP开发—JavaScript和QT对象交互应用程序开发
- HTTP工作原理
- 统计学发展方向的选择
- 类和对象课后作业
- Java语言使用注解处理器生成代码——第二部分:注解处理器
- 异步加载
- 如何随机取出两个数据之间的随机数
- opengl数据流向
- Kali Linux将Android设备变成黑客瑞士军刀
- 我的IOS学习历程-第七天
- 剑指offer第十六题【合并两个排序的链表】c++实现
- VirtualBox安装Fedora22后无法显示满屏
- 小白的笔记(1)----------------Makefile
- TCP四次挥手