Distance on Chessboard
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 26931 |
|
Accepted: 9124 |
Description Chess board is a black and white 8 * 8 square, pieces placed in the middle of the lattice. As shown in the following illustration:
The rules of the son, the rear, the car and the elephant are as follows:
Wang: Horizontal, straight, oblique can walk, but each step limited to walk a grid.
After: horizontal, straight, oblique can walk, the number of each step is unlimited.
Car: Horizontal, vertical can walk, not inclined to walk, the number of lattice unlimited.
Elephant: can only oblique walk, the number of lattice unlimited.
Write a program, given the starting position and target location, to calculate the minimum number of steps required for the king, rear, car, and image to go from the starting position to the target position.
The first line of Input is the number of groups of test data T (0 <= T <= 20). Each of the following lines is a set of test data, each of which includes two positions on the board, the first is the starting position, and the second is the target location. The position is expressed in the form "alpha-numeric", with letters from "a" to "H", and Numbers from "1" to "8".
Output the minimum number of steps required for each set of test data entered, such as the King, rear, car, and image. If it is not reachable, output "Inf".
Sample Input
2
A1 C3
f5 F8
Sample Output
2 1 2 1
3 1 1 Inf
Source POJ monthly--2004.05.15 Liu rujia@poj
problem-solving ideas: In fact, this problem, do not search, mathematical methods to calculate the distance can be too. Then, I want to mess up the test hackers, all use BFS. Three C⌒っ゚д゚) our store to deal with four kinds of pieces respectively, record the number of steps, found on the output.
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath
> #include <iomanip> #include <algorithm> #define MAXN #define INF 0xfffffff using namespace std;
struct node {int x, y;} s,e;
int STEP[MAXN][MAXN];
int VIS[MAXN][MAXN]; Search Direction int dir1[8][2]= {{ -1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}};//eight direction int dir2[4][2]= {{ -1,0},{0,1},{
1,0},{0,-1}};//Four direction straight go int dir3[4][2]= {{ -1,-1},{1,-1},{1,1},{-1,1}};//four direction oblique walk//Wang: Horizontal, straight, oblique can walk, but each step limited to one grid.
After: horizontal, straight, oblique can walk, the number of each step is unlimited.
Car: Horizontal, vertical can walk, not inclined to walk, the number of lattice unlimited. Elephant: can only oblique walk, the number of lattice unlimited.
*/void BFs () {bool flag=false;
Wang: Horizontal, straight, oblique can walk, but each step limited to one memset (step,0,sizeof (step));
memset (vis,false,sizeof (VIS));
queue<node>q;
Q.push (s);
Vis[s.x][s.y]=true;
while (!q.empty ()) {node T=q.front ();
Q.pop ();
for (int i=0; i<8; ++i) {Node A;
A.X=T.X+DIR1[I][0];
A.Y=T.Y+DIR1[I][1]; if (A.X>=0&&A.Y>=0&&A.X<8&&A.Y<8&&!VIS[A.X][A.Y]) {Q.push (a);
Vis[a.x][a.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
} if (a.x==e.x&&a.y==e.y) {vis[t.x][t.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
cout<<step[e.x][e.y]<< "";
flag=true;//mark can reach Goto B;
}}} if (!flag) cout<< "INF";//Cannot reach b://after: horizontal, straight, oblique can walk, the number of each step is unrestricted.
Flag=false;
memset (step,0,sizeof (step));
memset (vis,false,sizeof (VIS));
while (!q.empty ()) Q.pop ();//note emptying the queue Q.push (s);
Vis[s.x][s.y]=true;
while (!q.empty ()) {node T=q.front ();
Q.pop ();
for (int i=0; i<8; ++i) {Node A;
a.x=t.x+dir1[i][0];//First Take a step a.y=t.y+dir1[i][1]; while (1)//number of cells per step unrestricted {IF (a.x<0| | a.y<0| | a.x>=8| |a.y>=8) break;//exceeded limit if (a.x>=0&&a.y>=0&&a.x<8&&a.y<8&
; &!vis[a.x][a.y]) {Q.push (a);
Vis[a.x][a.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
} if (a.x==e.x&&a.y==e.y) {vis[t.x][t.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
cout<<step[e.x][e.y]<< "";
Flag=true;
Goto C;
} a.x+=dir1[i][0];//can continue to walk in that direction a.y+=dir1[i][1];
}}} if (!flag) cout<< "INF";
c://car: Horizontal, vertical can walk, not inclined to walk, the number of lattice unlimited.
Flag=false;
memset (step,0,sizeof (step));
memset (vis,false,sizeof (VIS));
while (!q.empty ()) Q.pop ();
Q.push (s);
Vis[s.x][s.y]=true;
while (!q.empty ()) {node T=q.front (); Q.pop ();
for (int i=0; i<4; ++i) {Node A;
A.X=T.X+DIR2[I][0];
A.Y=T.Y+DIR2[I][1]; while (1) {if (a.x<0| | a.y<0| |
A.x>=8| |a.y>=8) break;
if (A.x>=0&&a.y>=0&&a.x<8&&a.y<8&&!vis[a.x][a.y]) {
Q.push (a);
Vis[a.x][a.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
} if (a.x==e.x&&a.y==e.y) {vis[a.x][a.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
cout<<step[e.x][e.y]<< "";
Flag=true;
Goto D;
} A.x+=dir2[i][0];
A.Y+=DIR2[I][1];
}}} if (!flag) cout<< "INF";
D://Elephant: can only oblique walk, the number of lattice unlimited flag=false; memset (step,0,sizeof (step));
memset (vis,false,sizeof (VIS));
while (!q.empty ()) Q.pop ();
Q.push (s);
Vis[s.x][s.y]=true;
while (!q.empty ()) {node T=q.front ();
Q.pop ();
for (int i=0; i<4; ++i) {Node A;
A.X=T.X+DIR3[I][0];
A.Y=T.Y+DIR3[I][1]; while (1) {if (a.x<0| | a.y<0| |
A.x>=8| |a.y>=8) break;
if (A.x>=0&&a.y>=0&&a.x<8&&a.y<8&&!vis[a.x][a.y]) {
Q.push (a);
Vis[a.x][a.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
} if (a.x==e.x&&a.y==e.y) {vis[a.x][a.y]=true;
step[a.x][a.y]=step[t.x][t.y]+1;
cout<<step[e.x][e.y]<<endl;
Flag=true;
return; } A.x+=dir3[I][0];
A.Y+=DIR3[I][1];
}}} if (!flag) cout<< "INF" <<endl;
} int main () {#ifdef Online_judge #else freopen ("G:/cbx/read.txt", "R", stdin);
Freopen ("G:/cbx/out.txt", "w", stdout);
#endif Ios::sync_with_stdio (FALSE);
Cin.tie (0);
int T;
cin>>t;
while (t--) {char ch;
int t;
cin>>ch>>t;
s.y=ch-' a ', s.x=t-1;
cin>>ch>>t;
e.y=ch-' a ', e.x=t-1;
if (s.x==e.x&&s.y==e.y) cout<< "0 0 0 0" <<endl;//pay attention to the same end must be sentenced to the else BFS ();
} return 0;
}