Detailed solution for the minimum number of steps in yzoi2226, yzoi2226 limit solution

Source: Internet
Author: User

Detailed solution for the minimum number of steps in yzoi2226, yzoi2226 limit solution

Description-Problem Description

Among all kinds of chess games, the playing of chess pieces is always certain. For example, Chinese chess plays a "day ". One elementary school student thought that there would be two ways for the horse to become more interesting. Therefore, he stipulated that the horse could either follow the "day" or use the word "field" as in the same way. He enjoys go at the same table and finds it interesting afterwards. He wants to give it A try. He can choose two dots A, B, and A on the board (100*100, put the white child on point B, representing two horses. The pawnpiece can follow the word "day" or "Tian". The two men walk the dark horse and the white horse. Who uses the minimum number of steps to reach the point in the upper left corner of the coordinate (), who wins. Now he asks you for help. I will give you the coordinates of A and B. I want to know the possible minimum number of steps from two locations.

Input-Input data

Two rows: Coordinates of Point A in the first line, and coordinates of point B in the second line. The coordinates are all natural numbers not greater than 100.

Output-Output data

In the two rows, the minimum number of steps from the first line A to the position (); the second row is the minimum number of steps from the B to the position.

Detailed Solution

  After reading the question, we can know that the horse here can take the word "day" or "field". Then we assume that the coordinates of the point are (x, y ), then it can reach the coordinates of 12 cases, respectively: take the word "day" :( X-2, Y-1), (x-1, y-2), (X-2, y + 1 ), (x-1, y + 2), (x + 2, Y-1), (x + 1, y-2), (x + 1, y + 2), (x + 2, y + 1); take the word "Tian" :( X-2, y-2), (X-2, y + 2), (x + 2, y + 2), (x + 2, y-2 ). After this analysis, we can save all the increments of x and the corresponding increment of y to two arrays dx [], dy, move each time by selecting the corresponding elements of the array. The Code is as follows:

int dx[12]={-2,-2,-1,1,2,2,2,2,1,-1,-2,-2};int dy[12]={-1,-2,-2,-2,-2,-1,1,2,2,2,2,1};

After that, the question requires that two chess pieces be moved, and the final destination is (). So, do we need to discuss the moving method of each chess piece? In fact, even though the starting point is different for each piece, the final point is ). In this case, we can see that there is a piece starting from () and moving to (x1, y1), (x2, y2) Steps respectively. In this way, you can use bfs (Baidu defined: width-first search algorithm (also called breadth-first search) as one of the simplest graph search algorithms, this algorithm is also a prototype of many important graph algorithms. Dijkstra single-source shortest path algorithm and Prim Minimum Spanning Tree Algorithm both adopt the same idea as width-first search. Its alias, BFS, is a blind search method that systematically expands and checks all nodes in the graph for results. In other words, it does not take into account the possible location of the result. It searches the entire graph completely until the result is found .) To complete the minimum number of steps for the entire search.

Now that we have determined the bfs algorithm, we need to use the queue structure. We can use the queue in STL to define a queue array. The Code is as follows:

#include<queue>queue<int>q[4];

Q [1] stores the abscissa from (1, 1) to reaching the point, and q [2] stores the ordinate from (1, 1) to reaching the point, q [3] indicates the minimum number of steps required to reach this point. If you are not familiar with the queue in STL, you can also write the queue by yourself, as shown below:

int que[maxn][4]={0};

You also need to define a header pointer and a tail pointer. The initial values of both are 1 to indicate that the queue is empty. The Code is as follows:

int head=1,tail=1;

Second, for the convenience of the final output, we also need to define a s [maxn] [maxn] array to store the minimum number of steps that reach s [x] [y, finally, you only need to output the values of s [x1] [y1] And s [x2] [y2. At the same time, you must assign s [1] [1] to 0, and assign other elements to 1. The Code is as follows:

memset(s,-1,sizeof(s));s[1][1]=0;

(Next, we will use all our self-written queues for operations. The STL code will be provided at the end)

At the beginning, we joined the team in the initial status, that is, q [1] [1] = 1, q [1] [2] = 1, q [1] [3] = 0, the Code is as follows:

    q[1][1]=1;    q[1][2]=1;    q[1][3]=0;    

Next, we will use a loop to expand the operation. Like 1777 of the inverted water, the condition for loop establishment is head <= tail (that is, the queue is not empty ). then, we will discuss each of the 12 cases listed above, that is, using a for loop, from 0 to 11. the procedure is as follows: ① take the position that the first element can reach ② determine whether the current position is out of the border ③ determine whether the current position has reached (according to the principle of bfs, the number of previously reached steps must be smaller than the current number of steps, (4) update the current value of s [x] [y, that is, the value is equal to the value of the current q [head] [3] + 1 5. The new value of x and y is queued. 6. Check whether (x1, y1), (x2, the minimum number of steps in y2 is found, that is, to determine whether the values of s [x1] [y1], s [x2] [y2] are all greater than 0, if yes, the program will be output and the program will end. Otherwise, the first element of the Team will pop up (the first element is extracted, but not popped up) and be repeated. The Code is as follows:

1 while (head <= tail) 2 {3 for (int d = 0; d <12; d ++) 4 {5 int x = q [head] [1] + dx [d]; // The position where the first element of the team can reach the abscissa 6 int y = q [head] [2] + dy [d]; // The position where the first element of the team can reach the ordinate position 7 if (x> 0 & y> 0 & x <= 100 & y <= 100) // determine whether the field is in the boundary 8 if (s [x] [y] =-1) // determine whether 9 {10 s [x] [y] = q [head] [3] + 1 has been expanded; // update the value of s [x] [y] to 11 tail ++; // enter the new status to 12 q [tail] [1] = x; 13 q [tail] [2] = y; 14 q [tail] [3] = s [x] [y]; 15 if (s [x1] [y1]> 0 & s [x2] [y2]> 0) // determine whether the target State is reached 16 {17 cout <s [x1] [y1] <endl; // output 18 cout <s [x2] [y2] <endl; 19 return 0; 20} 21} 22} 23 head ++; // pop up the first element of the team 24}

Finally, the ac code on OJ is provided for reference only:

The following is a self-written queue:

1 # include <iostream> 2 # include <cstring> 3 using namespace std; 4 int const maxn = 10000 + 1; 5 int const area = 100 + 1; 6 int dx [12] = {-2,-2,-, 1,-1,-2,-2 }; 7 int dy [12] = {-1,-2,-2,-2,-2,-,-, 2, 2 }; 8 int s [area] [area], q [maxn] [4] = {0}; 9 int x1, y1, x2, y2; 10 int head = 1, tail = 1; 11 int main () 12 {13 memset (s, 0xff, sizeof (s); 14 cin> x1> y1> x2> y2; 15 q [1] [1] = 1; 16 q [1] [2] = 1; 17 q [1] [3] = 0; 18 while (head <= tail) 19 {20 for (int d = 0; d <12; d ++) 21 {22 int x = q [head] [1] + dx [d]; // obtain the position 23 int y = q [head] [2] + dy [d] where the first element of the team can reach the abscissa. // The position where the first element of the team can reach the ordinate value is 24 if (x> 0 & y> 0 & x <= 100 & y <= 100) // determine whether the boundary is 25 if (s [x] [y] =-1) // determine if 26 {27 s [x] [y] = q [head] [3] + 1 has been expanded; // update the value of s [x] [y] to 28 tail ++; // enter the new status to queue 29 q [tail] [1] = x; 30 q [tail] [2] = y; 31 q [tail] [3] = s [x] [y]; 32 if (s [x1] [y1]> 0 & s [x2] [y2]> 0) // determine whether the target State has been reached 33 {34 cout <s [x1] [y1] <endl; // output 35 cout <s [x2] [y2] <endl; 36 return 0; 37} 38} 39} 40 head ++; // pop up the first element 41} 42}

The following is a queue that uses STL directly. The operations for queuing are similar to those for the former, so we will not describe it again:

 1 /* 2     Name: lwq 3     Copyright:  4     Author:  5     Date: 14-12-14 14:17 6     Description: 2226STL 7 */ 8  9 #include<iostream>10 #include<cstring>11 #include<queue>12 using namespace std;13 int const maxn=10000+1;14 int const area=100+1;15 int dx[12]={-2,-2,-1,1,2,2,2,2,1,-1,-2,-2};16 int dy[12]={-1,-2,-2,-2,-2,-1,1,2,2,2,2,1};17 int s[area][area];18 queue<int>q[4];19 int x1,y1,x2,y2;20 int main()21 {22     memset(s,-1,sizeof(s));23     cin>>x1>>y1>>x2>>y2;24     q[1].push(1);25     q[2].push(1);26     q[3].push(0);27     while((!q[1].empty())&&(!q[2].empty())&&(!q[3].empty()))28     {29         for(int d=0;d<12;d++)30         {31             int x=q[1].front()+dx[d];32             int y=q[2].front()+dy[d];33             if(x>0&&y>0&&x<=100&&y<=100)34                 if(s[x][y]==-1)35                 {36                     s[x][y]=q[3].front()+1;37                     q[1].push(x);38                     q[2].push(y);39                     q[3].push(s[x][y]);40                     if(s[x1][y1]>0&&s[x2][y2]>0)41                     {42                         cout<<s[x1][y1]<<endl;43                         cout<<s[x2][y2]<<endl;44                         return 0;45                     }46                 }47         }48         q[1].pop();49         q[2].pop();50         q[3].pop();51     }52 }

Finally, we welcome your suggestions.

Related Article

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.