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.
#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
原创粉丝点击