P-残缺的棋盘
阅读原文时间:2023年07月10日阅读:2


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
分析:
BFS搜索题;
代码:

#include
#include
#include
#include

using namespace std;
const int N = + ;
typedef struct node{
int x,y,step;
bool operator <(const node x) const { return step > x.step;
}
}Node;
int x1,y1,x2,y2,x3,y3;

bool visit[N][N];
void Init(){
memset(visit,,sizeof(visit));
visit[x3][y3] = true;
}
const int dir[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
int BFS(){
priority_queue Q;
Node t,s;
t.x = x1,t.y = y1,t.step = ;
Q.push(t);
visit[t.x][t.y] = true;
while(!Q.empty()){
t = Q.top();Q.pop();
if(t.x == x2 && t.y == y2) return t.step;
for(int d=;d<;d++){ int newx = t.x + dir[d][]; int newy = t.y + dir[d][]; if(newx <= || newx > || newy <= || newy>|| visit[newx][newy] ) continue;
s.x = newx,s.y = newy,s.step = t.step+;
visit[s.x][s.y] = true;
Q.push(s);
}
}
return -;
}
int main(){
int Case = ;
while(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3){
Init();
printf("Case %d: %d\n",++Case,BFS());
}
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章