Patrol Robot UVa1600 patrol robot

Source: Internet
Author: User

Test instructions probably:

The robot is going from the upper-left corner (m*n) of a grid (with a range of M and n within a closed interval of 1 to 20) to the lower-right corner (M,n). Some grids in the grid are empty spaces, denoted by 0, other squares are obstacles, denoted by 1. The robot can walk in four directions at a time, but cannot traverse the K ([0,20]) barrier continuously to find the shortest path length. The starting and ending points are guaranteed to be open spaces.


Idea: With BFS search can, because can not continuously through the K barrier, so in the original vis2-dimensional array added 1 dimensions, into a 3-dimensional array, representing the number of layers (barriers) of the wall through.


The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <queue>using namespace std; int N,m,k,t;int Map[25][25];bool vis[25][25][25];//adds a dimension z to represent the number of layers of the skipped wall.    int dx[4]= {1,0,-1,0};int dy[4]= {0,1,0,-1};struct point{int x, y;    int step;    int layer;    Point (int x=1,int y=1,int step=0,int layer=0): X (x), Y (y), step (step), layer (layer) {}};int ans () {queue<point>q;    memset (vis,0,sizeof (VIS));    Point A (1,1,0,0);    Point End_point (N,M);    Q.push (a);    Vis[1][1][0]=true; while (!        Q.empty ()) {point Now=q.front ();        Q.pop ();        if (NOW.X==END_POINT.X&AMP;&AMP;NOW.Y==END_POINT.Y) return now.step;            for (int i=0; i<4; i++) {int x=dx[i]+now.x;            int y=dy[i]+now.y;            int layer=now.layer; if (Map[x][y]) layer++;//because it only moves one step at a time.                And met a wall, so gaga.            else layer=0; if (layer<=k&&!vis[x][y][layer]&&x>=1&&y>=1&&x<=n&&y<=m) {vis[x][y][layer]=true;            Q.push (Point (X,y,now.step+1,layer)); }}} return-1;}    int main () {cin>>t;        while (t--) {cin>>n>>m>>k;//input rows and columns and K values! for (int i=1, i<=n; i++) for (int j=1; j<=m; j + +) cin>>map[i][j];//input map cout&lt    ; <ans () <<endl; } return 0;}


Patrol Robot UVa1600 patrol robot

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.