UVA Patrol Robot (the shortest path of robot crossing obstacle BFS) experience in solving problems

Source: Internet
Author: User
Tags integer numbers

Original question:

Description

A robot have to patrol around a rectangular area which are in a form ofmxNGrid (mRows andNColumns). The rows is labeled from 1 tom. 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 , &NB Sp 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 positive integer numbers  m  AND&NB Sp 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 ijseparated 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 is the robot to reach the cell (m, n), write in one line T He integer number s, which 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 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

My Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 6 using namespacestd;7 8 intDirectionx[] = {1,0, -1,0 };9 intDirectiony[] = {0,1,0, -1 };Ten intm, N, K, able; One intcounts[ -][ -] = {0 }; A intmap[ -][ -] = {0 }; - intMin; -  the  -  - struct Location - { +     intx, y, step; -     intK; +Location (): X (1), Y (1), Step (0), K (0) {} A     BOOL operator= = (Location &b) {returnx = = B.x&&y = =b.y;} at }start,target; -  - BOOLCheck (location &a) - { -     if(a.x<1|| A.x>m | | a.y<1|| A.y>n)return 0; -     if(COUNTS[A.X][A.Y]! =0)             in         return 0; -     if(MAP[A.X][A.Y] = =1) to     { +         if(A.K <=0)return 0; -a.k--;return 1; the     } *     if(MAP[A.X][A.Y] = =0) $     {Panax NotoginsengA.K =able; -         return 1; the     } +  A } the  + intDFS (Location &start) - { $ Location old_location, new_location; $Queue<location>Q; -Old_location =start; - Q.push (old_location); theCOUNTS[OLD_LOCATION.X][OLD_LOCATION.Y] =1; -      while(!q.empty ())Wuyi     { theOld_location =Q.front (); - Q.pop (); Wu          for(inti =0; I <4; i++) -         { Aboutnew_location.x = old_location.x +Directionx[i]; $NEW_LOCATION.Y = Old_location.y +Directiony[i]; -NEW_LOCATION.K =OLD_LOCATION.K; -             if(!Check (new_location)) -                 Continue; A             if(New_location = =target) +                 returnOld_location.step +1; theCOUNTS[NEW_LOCATION.X][NEW_LOCATION.Y] =1; -New_location.step = Old_location.step +1; $ Q.push (new_location); the         } the     } the     return-1; the } -  in  the  the  About intMain () the { the     intT; theCIN >>T; +      while(t--) -     { theCin >> M >> n>>K;BayiTarget.x = m; Target.y =N; theSTART.K =K; theable =START.K; -Memset (Counts,0,sizeof(counts)); -memset (Map,0,sizeof(map)); the          for(inti =1; I <= m; i++) the         { the              for(intj =1; J <= N; J + +) the             { -CIN >>Map[i][j]; the             } the         } the         if(target = = start) Min =0;94         Else the         { theMin =dfs (start); the         }98cout << Min <<Endl; About     } -     101 102     return 0;103}

UVA Patrol Robot (the shortest path of robot crossing obstacle BFS) experience in solving problems

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.