UVA Patrol Robot

Source: Internet
Author: User
Tags integer numbers

Original question:
A robot have to patrol around a rectangular area which are in a form of M x N grid (m rows and N columns). The rows is labeled from 1 to M. The columns is 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 have 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 this 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 was a positive integer and is not bigger than 20. The following lines describe the data sets.
For each data set, the first line contains a 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 are an obstacle on the cell (I, j), and is 0 otherwise.
Output
For each data set, if there exists a is the robot to reach the cell (M, n), write in one line the integer number s, W Hich is the number of moves the robot have 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

English:
There is a robot walking in a picture, the figure 1 represents a barrier, 0 means no obstacle. Now the robot wants to go to (n,m) point from (the) point. But the robot cannot go through the K-barrier continuously . Now ask you what the shortest path is. If the output is not reached-1

#include <bits/stdc++.h> using namespace std;
int t,n,m,k;
struct Point {int X,Y,KK,PACE;//KK indicates that a number of obstacles have been continuously traversed, pace is a step};
int move[5][2]={{1,0},{-1,0},{0,1},{0,-1}};
int g[25][25]; BOOL vis[25][25][21];//more than one state bool judge (point x)//Whether out of bounds {if (x.x>=1&&x.x<=n&&x.y>=1&&
    AMP;X.Y&LT;=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])//Whether there is a barrier tmp.kk++;//a barrier KK increments
                else tmp.kk=0;//recover to 0 if (tmp.kk>k) continue without obstacles;
     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++;
        } else vis[1][1][0]=1;
        int Ans=bfs (start);
    cout<<ans<<endl;
} return 0;
 }

Answer:
Purple books inside the exercises, very obvious wide search. The topic is not difficult, in the graph more record a state Vis[x][y][k] indicates at coordinates (x, y) when the state through continuous through the K-barrier is saved. Then the direct wide search is good ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.