Minimum number of steps

Source: Internet
Author: User

Minimum number of steps time limit: theMs | Memory Limit:65535KB Difficulty:4
Describe

This has a maze, with 0~8 rows and 0~8 columns:

1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,1

0 represents the road, and 1 represents the wall.

Now enter the coordinates of a road as the starting point, and then enter a road as the coordinates as the end point, ask at least a few steps to reach the end from the starting point?

(Note: One step is to move from one sitting punctuation to the next to the left and right adjacent coordinate points, such as: from (3,1) to (4,1). )

Input
the first line enters an integer n (0<n<=100), which indicates that there are n sets of test data;
Then n rows, each row has four integers a,b,c,d (0<=a,b,c,d<=8) representing the row, column, and end line of the starting point, respectively.
Output
The
output is at least a few steps away.
Sample input
1  5 1  6 7
Sample output
1211
Source
[Miao Building] Original
Uploaded by

Miao-dong Building

Your own search is not very good, reference code address: 54 Floor

http://acm.nyist.net/JudgeOnline/talking.php?pid=58

#include <iostream> #include <queue> #include <string.h>using namespace Std;int num[9][9]={ 1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0, 0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1};int visit [9] [9];struct point{int x;int y;int Step;};   void BFs (int startx,int starty,int endx,int endy) {queue<point>q;        Point Now,next;int x[4]={0,1,0,-1};        int Y[4]={1,0,-1,0};int k,exit=0;    Now.step=0;now.x=startx;now.y=starty;    Q.push (now);        while (!q.empty ()) {Next=q.front (); Q.pop ();   if (Next.x==endx &&next.y==endy) {exit=1; break;} for (k=0;k<4;k++) {now.x=next.x+x[k];now.y=next.y+y[k];if (!visit[now.x][now.y] &&now.x>=0 && Now.x<9 &&now.y>=0 &&now.y<9 &&num[now.x][now.y]==0) {visit[now.x][now.y]= 1;now.step=next.step+1;q.push (now);}}} if (exit) Cout<<next.step<<endl;elsecout<<-1<<endl;}   int main () {int A,b,c,d,t;cin>>t;while (t--) {cin>>a>>b>>c>>d;   memset (visit,0,sizeof (visit));  BFS (A,B,C,D);         } return 0;}

Minimum number of steps

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.