Title Description
Problem D:knight ' s trip
In chess, each move of a knight consists of moving by, squares horizontally and one square vertically, or by one square Horizontally and squares vertically. A Knight making one move from location (0,0) of an infinite chess board would end up at one of the following eight Locatio NS: (+), ( -1,2), (1,-2), ( -1,-2), (2,1), ( -2,1), (2,-1), ( -2,-1).
Starting from location (0,0), what's the minimum number of moves required for a knight to get to some other arbitrary loc ation (x, y)?
Input requirements
Each line of input contains the integers x and y, each with absolute value is at the most one billion. The integers designate a location (x,y) on the infinite Chess board. The final line contains the wordEND.
Output requirements
For each location in the input, output a line containing one integer, the minimum number of moves required for a knight to Move from (0,0) to (x,y).
If the input
1 4END
should be output
12
This problem can not be used BFS,BFS code as follows, because the chessboard is very large, will not be enough space, but can be used to print a chessboard of BFS to analyze the situation, the problem has been done for a long time--、、、、
#include <iostream> #include <algorithm> #include <vector> #include <string.h> #include < Ctype.h> #include <math.h>using namespace Std;int map[1005][1005];int res[1005][1005];struct point{int x, y;}; struct point r[1005];int dis[8][2]={{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}};int m,n;void BFS () {int Tail=1, head=0,i,x1,y1;r[0].x=2;r[0].y=2;while (tail! = Head) {x1=r[head].x;y1=r[head].y;for (i=0; i<8; i++) {x1+= Dis[i][0], y1+=dis[i][1];if (map[x1][y1]==0&&res[x1][y1]==-1) {R[tail].x=x1;r[tail].y=y1;res[x1][y1] = 1 + res[x1-dis[i][0]][y1-dis[i][1]];tail++;} X1-=dis[i][0], y1-=dis[i][1];} Head++;if (Res[m+2][n+2]!=-1) break;}} void Fun (), int main () {fun (); return 0;} void Fun () {int I,j;while (cin>>m>>n) {memset (map,-1,sizeof (map)); memset (res,-1,sizeof (res)); M=abs (m); n =abs (n); if (m>n) {int temp=m;m=n;n=temp;} for (i=2;i<m+3;i++) {for (j=2;j<n+3;j++) map[i][j]=0;} res[2][2]=0;//chessboard 0, 0 res[3][3]=2;//chessboard 1, 1 coordinates to own the value, otherwise the BFS calculated is 4 is wrong, because we put the chessboard only to considerInto the first quadrant of the BFS (); for (i=2;i<m+3;i++) {for (j=2;j<n+3;j++) printf ("%3d", Res[i][j]); Cout<<endl;} Cout<<res[m+2][n+2]<<endl;}}
can be resolved with enumerations, as follows
N=2*m This situation is directly (M+N)/3 steps.
If N<2*m but (m+n)%3==0, then we can control (by), (2,1) The number of two hops to reach ... The total number must be (m+n)/3, then m,n and 3 to the remainder is 1, we are not necessarily in (m+n-1)/3 steps to jump to (m,n-1) this point, but can not jump to (m,n), two steps back to (m-4,n-5) This point, we can use three steps to jump to (m,n ), then the original number of steps is +1. The remainder is 2, which is to jump first to (m-1,n-1) This place, we know (0,0) to (to) only two steps, then (m-1,n-1) to (M,n) is the original step number +2.
Then consider N>2*m, first of all (0,1) situation special treatment. Then we can jump to (m,n) with M step, then the original problem is converted to (0,0) to (0,n-2*m). When N-2*m is a multiple of 4 we can directly ( -1,2) This jump can arrive at (N-2*M)/2 steps. The remainder is 1, that is (0,0) to (0,1) the problem, but this need three steps is not optimal, we withdraw two steps into (0,0) to (0,5), we can achieve three steps, then is the original number of steps plus 1 is the solution. The remainder is 2, and we can jump once (2,1) ( -2,1) to arrive. The remainder is 3, converted to (0,0) to (0,3) The problem we Can ( -1,2) () (0,3) Three steps to arrive.
#include <iostream> #include <algorithm> #include <vector> #include <string.h> #include < Ctype.h> #include <math.h>using namespace std;void fun (), int main () {fun (); return 0;} void Fun () {int M,n;while (cin>>m>>n) {m=abs (M); N=abs (n); if (m>n) {int temp=m;m=n;n=temp;} if (2*m>=n) {if (m==n&&m==1) Cout<<2<<endl;else if (m==n&&m==2) cout<<4<< endl;elsecout<< (m+n)/3+ (m+n)%3<<endl;} Else{int ans=m;int c= (n-2*m)%4;ans+=c;ans+= (n-2*m-c)/2;if (n==1&&m==0) Ans=3;cout<<ans<<endl;}}
Knight's trip horse jumps to the minimum number of steps in the specified point on the wireless large board