Patrol robot.

Source: Internet
Author: User
Tags integer numbers

Test Instructions:

The robot is going from the upper-left corner (m*n) of a grid (1<=M,N<=20) to the lower-right corner (M,n). Some grids in the grid are empty spaces (denoted by 0) and other squares are obstacles (denoted by 1). The robot can walk in four directions at a time, but cannot traverse the K (0<=k<=20) barrier continuously, seeking the shortest length. The starting and ending points are guaranteed to be open spaces. The shortest length is 10.

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 the positive integer numbersmandNSeparated by Space(1m,N). The second line contains an integer numberk(0k). TheIThLine of the nextmLines containsNInteger a ij separated by Space  I  = 1, 2,..., &NBSP; m ; J  = 1, 2,..., n ). The value Of  a ij  is&NBSP; 1  if There is a obstacle on the Cell  i , &NBSP; j ), and is &NBSP; 0  otherwise.

Output

for each data set, if there exists a, the robot to reach the Cell  m , &NBSP; n ), write in one line the integer number< Span class= "Apple-converted-space" >  s , which is the number of moves the robot have to make ; &NBSP; -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 00 0 1 0 1 10 1 1 1 1 00 1 1 1 0 02 2 0 0 1 1 0

Sample Output

7 10-1

Thinking Analysis:

Find the optimal solution, adopt BFS.

Prerequisite Preparation:

1, the first input K value is the robot's blood volume, continuously through the k+1 obstacle, the robot will hang off. If, however, the first successive crossing

K-1 a barrier and then encounter 0, then you need to fill the robot blood.

2, a three-dimensional array to mark the robot has been here, can be assigned to a value of 1. The third dimension of data is indicated by the amount of blood left on the road by the robot.

3, the queue to store each node, (that is, each center, because the robot each to a place (center) there are four directions to walk)

Analysis:

1, the simulation robot Patrol, encounters "1" to reduce the blood quantity.

2, each to arrive at a point, need to judge: whether the point is out of bounds (to change direction), whether the point is the end (direct end), whether the point before (change direction)

3, put the points can be left in the queue, continue the process above.

Source:

1#include <iostream>2#include <queue>3#include <cstring>4 #defineMAXN 255 using namespacestd;6 intto[4][2] = { -1,0,0, -1,1,0,0,1 };7 intN, M, Z;8 intCHESS[MAXN][MAXN];9 intDOT[MAXN][MAXN][MAXN];Ten structnode{ One     intx; A     inty; -     intZ; -     intStep; the }r,r1,t; - intJudgeintAintb) - { -     if(A >= n | | A <0|| b >= m | | b <0 ) +         return 1; -     return 0; +  A } at intBFS () - { -Queue<node>Q; -R.x =0; -R.Y =0; -R.z =Z; inR.step =0; -dot[0][0][Z] =1; to Q.push (R); +      while(!q.empty ()) -     { thet =Q.front (); * Q.pop (); $         if(T.x = = N-1&& T.y = = M-1)//arrive at your destinationPanax Notoginseng             returnT.step; -          for(inti =0; I <4; i++) the         { +r1.x = T.x + to[i][0];  AR1.Y = T.y + to[i][1];//* before is [0][i] the  +             if(r1.x = = N-1&& R1.y = = M-1) -                 returnT.step +1; $             if(Judge (r1.x, R1.y))//Cross the line and change direction. $                 Continue; -  -             if(Chess[r1.x][r1.y])//* Before is dot[][] theR1.z = t.z-1;//* Before the z-1, minus one means the robot's blood volume -             ElseWuyiR1.z =Z; the             if(R1.z <0)     -                 Continue; Wu             if(Dot[r1.x][r1.y][r1.z])//* It was wrong to put it in a cross-border place, where z is not yet assigned . -                 Continue;//Judging the robot walking, change direction AboutDOT[R1.X][R1.Y][R1.Z] =1;//mark the robot through here $R1.step = T.step +1;//* - Q.push (R1); -  -         } A      +     if(Q.empty ())//* put it in the loop before * the     { -  $T.step =-1; the         returnT.step; the     } the } the } - intMain () in { the     intT; theCIN >>T; About      while(t--) the     { thememset (chess,0,sizeof(chess)); thememset (Dot,0,sizeof(dot)); +CIN >> N >>m; -CIN >>Z; the          for(inti =0; I < n; i++)Bayi         { the              for(intj =0; J < M; J + +) theCIN >>Chess[i][j]; -         } -cout << BFS () <<Endl; the          the  the     } the     return 0; -}

Experience:

This is the second time with the BFS, do not know, did only to find the last time the understanding of BFS is not very deep, resulting in a variety of errors in the code (* mark), the header file will also forget, CString. This time to finish this topic, carefully read over, more profound than the last understanding, with BFS to pay attention to the initialization, cross-border problems, the definition of the variable number of problems, there are those variables followed by which point, of course, there is a problem of zeroing out. The problem of this discovery is more, in short, refueling it!

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.