您的位置:首页 > 其它

csu1511——残缺的棋盘(bfs)

2017-05-02 16:47 330 查看


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 <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <ctime>
#define INF 0x3f3f3f3f
#define MAXN 100005
#define Mod 1000000007
using namespace std;
int r1,c1,r2,c2,r3,c3;
int vis[10][10];
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,1,-1,-1};
struct Node
{
int x,y,step;
};
int bfs(int x,int y)
{
Node start;
start.x=x,start.y=y;
start.step=0;
vis[x][y]=1;
queue<Node> q;
q.push(start);
while(!q.empty())
{
Node tmp=q.front(),tmp1;
q.pop();
if(tmp.x==r2&&tmp.y==c2)
{
return tmp.step;
}
for(int i=0;i<8;++i)
{
tmp1=tmp;
int xx=tmp1.x+dx[i];
int yy=tmp1.y+dy[i];
if(xx<1||xx>8||yy<1||yy>8||vis[xx][yy])
continue;
vis[xx][yy]=1;
tmp1.x=xx;
tmp1.y=yy;
tmp1.step++;
q.push(tmp1);
}
}
}
int main()
{
int cnt=1;
while(~scanf("%d%d%d%d%d%d",&r1,&c1,&r2,&c2,&r3,&c3))
{
memset(vis,0,sizeof(vis));
vis[r3][c3]=1;
printf("Case %d: %d\n",cnt++,bfs(r1,c1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: