X small solution to a problem with the robot (betago)

Title Description

X very little interest in recent victory over South Korea Go Great God Li Shishi AlphaGo, so small X wrote a program called BetaGo of artificial intelligence (abbreviated AI), BetaGo what this will do it?
First of all we want to make a small X BetaGo do their own on the board Lazi, it AlphaGo is done by the programmer. Imagine a small X is this: Place a small robot on the board of the border, this little robot will move along the board to the position closest to the border point of Lazi, then stretched out its robotic arm to put the pieces on the board. The most critical step is how to make it inside the small mobile robot along the shortest path on the board of the border, a small X would like to invite you to help him compile a program to solve this problem.
It is well known Go board size 19 × 19 (shown below), is the circle in FIG bold border.
We use a pair of integers (x, y) to represent the first board x horizontal lines (the number of from the bottom) and the y-th vertical line (from left to right number) of the intersection, as the figure above frame A with point (6, 1), B by points (10, 19), placed in a small robot (x1, y1) in this position initially, it wants to move to (x2, y2) in this position. (X1, y1) and (x2, y2) must be on the intersection board small border each step from the current position of the robot can be moved to a position adjacent (vertical and horizontal) on, i.e. from every (x, y ) to (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1) of a four positions, but it can not get out or into the board, which means it can only move along the border of the board to an adjacent position, which means that at any one time only two adjacent positions are exactly.
BetaGo will tell little robot requires a minimum number of steps to go, but still very small X BetaGo worried sometimes get out of control, so he told an incorrect value.
For this reason small X had to help you, I hope you compile a program to calculate how many steps from (x1, y1) to move the board along the border to (x2, y2) requires a minimum of walking. From the figure above point A (6, 1) to point B (10, 19) take a minimum of 32 steps, the movement route is :( 6, 1) → (5, 1) → (4, 1) → (3 , 1) → (2, 1) → (1, 1) → (1, 2) → (1, 3) → ...... → (1, 19) → (2, 19) → ...... → (10, 19 )

Entry

Only one line of input data comprising four space-separated integer representation x1, y1, x2, y2.
Guaranteed data (x1, y1), (x2, y2) must cross the border point on the board.

Export

Output line contains an integer ans, represents a small robot from (x1, y1) to move to (x2, y2) minimum steps.

Sample input 

6 1 10 19

Sample Output 

32

【data range】

For 30% of the data, (x1, y1), (x2, y2) on the same frame
For another 30% of the data, (x1, y1), (x2, y2) on two adjacent frame
For another 40% of the data, (x1, y1), (x2, y2) on the two opposite border
Time limit: 1s
Space limitations: 256M
 

answer

Method One: Category talk

three conditions:

(1) two opposite points, to compare two routes

(2) the adjacent two points on the straight line (x abscissa y ordinate) abs (x1-x2) + abs (y1-y2)

(3) the same line abs (x1-x2) or abs (y1-y2)

Reference Code

 1 #include<bits/stdc++.h>
 2 #define int long long
 3 using namespace std;
 4 int solve(int x,int y){
 5     if(x-y>=0) return x-y;
 6     return y-x;
 7 }
 8 signed main()
 9 {
10     //freopen(".in","r",stdin);
11     //freopen(".out","w",stdout);
12     int x1,y1,x2,y2,a,b;
13     scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
14     if(abs(x1-x2)==18){
15         a=18+y1-1+y2-1;
16         b=18+19-y1+19-y2; 
17     }
18     else if(abs(y1-y2)==18){
19         a=18+x1-1+x2-1;
20         b=18+19-x1+19-x2; 
21     }
22     else if(x1==x2) a=solve(y1,y2);
23     else if(y1==y2) a=solve(x1,x2);
24     else a=solve(x1,x2)+solve(y1,y2);
25     if((a>b)&&(b!=0)) printf("%lld\n",b);
26     else printf("%lld\n",a);
27     return 0;
28 }

Method Two: breadth-first search (BFS)

Reference Code

 1 #include<bits/stdc++.h>
 2 #define int long long
 3 using namespace std;
 4 queue<int>x,y,sum;
 5 int vis[20][20],sx,sy,tx,ty,xx,yy,summ;
 6 signed main()
 7 {
 8     //freopen(".in","r",stdin);
 9     //freopen(".out","w",stdout);
10     scanf("%lld%lld%lld%lld",&sx,&sy,&tx,&ty);
11     if(sx==tx&&== 12ty) {
         printf("0\n");
13         return 0;
14     }
15     x.push(sx),y.push(sy),sum.push(0);
16     while(!x.empty()){
17         xx=x.front(),yy=y.front(),summ=sum.front();
18         vis[xx][yy]=summ;
19         if(xx==1||xx==19){
20             if(yy<19&&vis[xx][yy+1]==0)
21                 x.push(xx),y.push(yy+1),sum.push(summ+1);
22             if(yy>1&&vis[xx][yy-1]==0)
23                 x.push(xx),y.push(yy-1),sum.push(summ+1);
24         }
25         if(yy==1||yy==19){
26             if(xx<19&&vis[xx+1][yy]==0)
27                 x.push(xx+1),y.push(yy),sum.push(summ+1);
28             if(xx>1&&vis[xx-1][yy]==0)
29                 x.push(xx-1),y.push(yy),sum.push(summ+1);
30         }
31         if(xx==sx&&yy==sy) vis[xx][yy]=1;
32         x.pop(),y.pop(),sum.pop();
33     }
34     printf("%lld\n",vis[tx][ty]);
35     return 0;
36 }

 

Guess you like

Origin www.cnblogs.com/maoyiting/p/betago.html