[BFS] [medium difficulty] wikioi3055 bronze lotus pond

Source: Internet
Author: User
3055 bronze lotus pond Title Description Description

To entertain and exercise the cows, Farmer John built a beautiful pond. The pool of this rectangle is divided into n columns (1 ≤ m, n ≤ 30) in m rows ). Some grids are solid, surprising Lotus, and some grids are rocks, while others are beautiful, pure, blue water. Bessie is practicing ballet. She is standing on a lotus flower and wants to jump to another lotus flower. She can only jump from one lotus flower to another lotus flower, neither jumping to the water, or jump to the rock.
Bessie's dance steps are similar to the horse steps in chess: M1 (1 ≤ M1 ≤ 30) cells are always moved horizontally each time, and M2 (1 ≤ M2 ≤ 30, M1 =m2) cells are moved vertically, or move the M1 lattice vertically and then the M2 lattice horizontally. Up to eight moving directions are available for Beixi. Given the layout of the pond and the Skip length of Beixi, calculate the minimum number of steps from the start point to the destination. We ensure that the destination in the input data is reachable.

Input description Input description

The first line: Four integers separated by spaces: m, n, M1, and m2
Line 2 to line m + 1: line I + 1 has n integers separated by spaces, describing the status of line I in the pond: 0 is water, 1 is Lotus, 2 is the rock, 3 is the start point of Beixi, and 4 is the end point of Beixi.

Output description Output description

Row 1: the minimum number of steps from the start point to the end point.

Sample Input Sample Input

4 5 1 2
1 0 1 0 1
3 0 2 0 4
0 1 2 0 0
0 0 0 1 0

Sample output Sample output

2

Data range and prompt Data size & hint

 

[Example]
Bessie starts from the leftmost side of the second row, and the target is the rightmost side of the second row.
Bessie first jumps to the Lotus in the third column of the first row, and then to the end. Two steps are required.

The reason is bronze lotus pond, because it is bronze, and we also have silver group, Gold Group!

Thinkings
This is the most basic BFs. I don't seem to be able to use STL queues, but it should not be too difficult to write a hand .. It took so long .. Or the last reason: CIN> m> N; the condition for determining whether the boundary overflows is: If (x> 0) & (x <= m) & (Y> 0) & (Y <= N) & (not crossed) & (lutong )){...... } Details ...... ... P.s.: Didn't you say you have a gold group or a silver group? Where is it?
Code
 1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 using namespace std; 5 struct note 6 { 7        int x,y,step; 8 }; 9 int N,M,M1,M2;10 int xx[10];11 int yy[10]; 12 int board[50][50];13 int gone[50][50]={0};14 note queue[10005];15 int startx,starty,ansx,ansy,qh,qt;16 note now,next;17 void readin()18 {19      cin>>M>>N>>M1>>M2;20      for (int i=1;i<=M;i++)21      { 22        for (int j=1;j<=N;j++) 23       {24         cin>>board[i][j];25         gone[i][j]=0;26         if (board[i][j]==3) {startx=i;starty=j;board[i][j]=1;gone[i][j]=1;} 27         if (board[i][j]==4) {ansx=i;ansy=j;board[i][j]=1;gone[i][j]=0;}28       }29      } 30      xx[1]=M1; xx[2]=M2; xx[3]=M2; xx[4]=M1; xx[5]=-M1; xx[6]=-M2; xx[7]=-M2; xx[8]=-M1; 31      yy[1]=M2; yy[2]=M1; yy[3]=-M1; yy[4]=-M2; yy[5]=-M2; yy[6]=-M1; yy[7]=M1; yy[8]=M2;32     now.x=startx;now.y=starty; now.step=0;33     queue[1]=now;34     qh=1;qt=1;;35 }36 37 int main()38 {39     readin();40     while ( qh<=qt )41     {42         now=queue[qh];43         qh++;44         if ( (now.x==ansx) && (now.y==ansy) )  { cout<<now.step<<endl; return 0; }45         next.step=now.step+1;46         for (int i=1;i<=8;i++)47         {48             next.x=now.x+xx[i];49             next.y=now.y+yy[i];50             if ( (next.x==ansx) && (next.y==ansy) )  { cout<<next.step<<endl; return 0; }51             if ( (next.x>0) && (next.x<=M) && (next.y>0) && (next.y<=N) && (board[next.x][next.y]==1) && (gone[next.x][next.y]==0))52             {53                    gone[next.x][next.y]=1;54                    qt++;55                    queue[qt]=next;56             }57         }   58     }59               60     return 0;    61 }
Result of the AC code of the bronze Lotus Pool

 

P.s.: Well, it's still time consuming. After all, I'm stupid ......

 

 

Framework

Well, give yourself a framework .. After all, search is the most difficult algorithm in the world-a type of brute force.

Struct note {int X, Y, step;}; note queue []; int main () {CIN> m> N; Various reads; queue [1]. X = startx; queue [1]. y = starty; queue [1]. step = 0; head = 1; tail = 1; while (Head <= tail) {If (matching answer) {cout <queue [head]. step; return 0 ;}for (I = 1; I <= Number of cases; I ++) {x = queue [head]. X + movex [I]; y = queue [head]. X + Movey [I]; If (x> 0) & (x <= m) & (Y> 0) & (Y <= N) & (not crossed) & (lutong) {tail + = 1; queue [tail]. X = x; queue [tail]. y = y; queue [tail]. step = queue [head]. step + 1 ;}} head + = 1 ;}

 

 

 

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.