Sicily Maze (BFS Compute Maze Shortest path)

Source: Internet
Author: User

Maze.
Time Limit:1sec Memory limit:256mbdescription

Master Tang is kidnapped by a monster and put in a maze, and Mr. Sha wants to rescue him.

The maze is an n*m matrix. There is types rooms in the maze, 1 for safe and 0 for dangerous. Of course, Mr Sha can only be stay in the safe. Mr. Sha is today in the top left corner. The lower right corner guest (N,M) Mr. Sha can move to another safe, and down, left or right direction. Each move takes 1 unit of time. Mr. Sha wants to find Master Tang as soon as possible. Can-You-help Mr. Sha to count the least time-to-get to the "where Master Tang is?" Input

The first line contains an integer t (0<t<=10), which means t test cases followed.

The first line of all test case contains 2 integers n (1≤n≤120) and M (1≤n≤120), which is the size of the maze. The next n lines input the maze, each line contains m number (0 or 1) seperated by one space. The next line contains 2 integers x (1≤n≤120) and Y (1≤n≤120), which means Master Tang are in the (x, y). Output

For each test case, the output of the answer in a line:the least time for Mr. Sha to get to Master Tang. If Mr. Sha can ' t get to Mast Tang, output-1.

Sample Inputcopy sample input to Clipboard
22 21 00 12 24 4 1 1 1 01 0 1 11 0 1 11 1 1 03 4
Sample Output
-15

Title: Starting from (a), go to the end of the input to save people. In the middle of the room, 1 is safe, 0 means danger.
If it can be reached, go to the shortest path. cannot be output-1.
This is a maze of the topic, simple calculation of the shortest path can be. But stuck inside the queue. Lead to wasted time.
You can do it with a vector.

1#include <iostream>2#include <cmath>3#include <queue>4#include <vector>5 6 using namespacestd;7 8 structnode9 {Ten     intx; One     inty; A }; -  - //Four Directions the intmove[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; - intn,m,room[1001][1001],x,y; -  - //is this the way to go ? + BOOLSafeintXinty) - { +     if(X >0&& y >0&& x <= n && y <= m && room[x][y] = =1) A     { at         return true; -     } -     return false; - } -  - intMain () in { -     intT; to      +Cin>>T; -      the      while(t--) *     { $Cin>>n>>m;Panax NotoginsengVector <node> v[1001]; - node No; the          +          for(inti =1; I <= N; i++) A         { the              for(intj =1; J <= M; J + +) +             { -Cin>>Room[i][j]; $             } $         } -Cin>>x>>y; -no.x =1; theNO.Y =1; -v[0].push_back (no);Wuyi         intK =0; the         intFlag =0; -         //use vector to do breadth First search Wu          while(1) -         { About             if(V[k].begin () = =v[k].end ()) $             { -                  Break; -             } -Vector <node>:: iterator it; A              for(It=v[k].begin (); It!=v[k].end (); it++) +             { theNode F = *it; -                  for(inti =0; I <4; i++) $                 { the node tmp; thetmp.x = f.x + move[i][0]; theTMP.Y = F.y + move[i][1]; the                     if(tmp.x = = x && tmp.y = =y) -                     { inFlag =1; the                          Break; the                     } About                     if(Safe (tmp.x,tmp.y)) the                     { theV[k +1].push_back (TMP); theROOM[TMP.X][TMP.Y] =0; +                     } -                 } the             }Bayik++; the             if(Flag = =1) the             { -                  Break; -             }     the         } the         if(flag) the         { thecout<<k<<Endl; -         } the         Else the         { thecout<<"-1"<<Endl;94         } the          the     } the     return 0;98}

Sicily Maze (BFS Compute Maze Shortest path)

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.