本文主要是介绍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 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 a ij separated by space (i = 1, 2,…, m;j = 1, 2,…, n). The value of a ij 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
中文:
有个机器人在一个图中走,图中1表示障碍,0表示没有障碍。现在这个机器人想从(1,1)点走到(n,m)点。不过这个机器人不能连续的穿过k个障碍。现在问你最短路径是多少?如果不能到达输出-1
#include <bits/stdc++.h>
using namespace std;
int t,n,m,k;
struct point
{int x,y,kk,pace;//kk表示已经连续穿过了几个障碍,pace为步数
};
int Move[5][2]={{1,0},{-1,0},{0,1},{0,-1}};
int G[25][25];
bool vis[25][25][21];//多存一个状态
bool judge(point x)//是否越界
{if(x.x>=1&&x.x<=n&&x.y>=1&&x.y<=m)return true;return false;
}
int bfs(point start)
{queue<point> Q;Q.push(start);memset(vis,false,sizeof(vis));while(!Q.empty()){point head=Q.front();Q.pop();for(int i=0;i<4;i++){point tmp;tmp.x=head.x+Move[i][0],tmp.y=head.y+Move[i][1],tmp.kk=head.kk,tmp.pace=head.pace+1;if(judge(tmp)){if(G[tmp.x][tmp.y])//是否有障碍tmp.kk++;//有障碍kk递增elsetmp.kk=0;//没有障碍就恢复到0if(tmp.kk>k)continue;if(vis[tmp.x][tmp.y][tmp.kk])continue;if(tmp.x==n&&tmp.y==m)return tmp.pace;vis[tmp.x][tmp.y][tmp.kk]=1;Q.push(tmp);}}}return -1;
}
int main()
{ios::sync_with_stdio(false);cin>>t;while(t--){cin>>n>>m>>k;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>G[i][j];point start;start.x=1,start.y=1,start.kk=0,start.pace=0;if(G[1][1]){vis[1][1][1]=1;start.kk++;}elsevis[1][1][0]=1;int ans=bfs(start);cout<<ans<<endl;}return 0;
}
解答:
紫书里面的练习题,很明显的广搜。题目不难,在图中多记录一个状态vis[x][y][k]表示在坐标(x,y)的时候穿过连续穿过k个障碍的的状态是否保存过。然后直接广搜就好~
这篇关于uva 1600 Patrol Robot的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!