Description
Input
输入包含不超过10000 组数据。每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同。
Output
对于每组数据,输出测试点编号和最少步数。
Sample Input
1 1 8 7 5 6 1 1 3 3 2 2
Sample Output
Case 1: 7 Case 2: 3
代码:
#include<cstdio> #include<queue> #include<iterator> using namespace std; int dis[9][9]; int r1,c1,r2,c2,r3,c3; int x[8] = {0,1,1,1,0,-1,-1,-1}; int y[8] = {1,1,0,-1,-1,-1,0,1}; struct Point{ int x; int y; }point[9][9]; void init(){ for(int i = 1;i <= 8;i++){ for(int j = 1;j <= 8;j++) { point[i][j].x = i; point[i][j].y = j; } } } int bfs(){ bool flag = false; queue<struct Point> q; q.push(point[r1][c1]); while(!flag){ struct Point temp = q.front(); q.pop(); for(int i = 0;i < 8;i++){ if(temp.x + x[i] >= 1 && temp.x + x[i] <= 8 && temp.y + y[i] >= 1 && temp.y + y[i] <= 8 && dis[temp.x][temp.y] != -1 && !(temp.x + x[i] != r3 && temp.y + y[i] != c3)){ q.push(point[temp.x + x[i]][temp.y + y[i]]); dis[temp.x + x[i]][temp.y + y[i]] = dis[temp.x][temp.y] + 1; if(temp.x + x[i] == r2 && temp.y + y[i] == c2) return dis[temp.x + x[i]][temp.y + y[i]]; } } } return 0; } int main(){ init(); int n = 1; while(scanf("%d %d %d %d %d %d",&r1,&c1,&r2,&c2,&r3,&c3) == 6){ for(int i = 1;i <= 8;i++){ for(int j = 1;j <= 8;j++) dis[i][j] = -1; } dis[r1][c1] = 0; printf("Case %d: %d ",n++,bfs()); } return 0; }