您的位置:首页 > 其它

ACM--BFS--残缺的棋盘--CUS 1511--第十届湖南省赛

2016-04-27 16:42 471 查看
1511: 残缺的棋盘

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 169 Solved: 56

[Submit][Status][Web Board]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

================================华丽的分割线===========================

这个是湖南省第十届省赛的一道简单的搜索题目,直接BFS就可以求出来。但是写的时候要注意一些细节

#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
int visited[205][205];
int move[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,-1},{1,1},{-1,1},{-1,-1}};//周围的八个方向跳
int result_x,result_y,ob_x,ob_y;
struct node{
int x,y;
int step;
};
//用来判断位置是否合法
bool check(int x,int y){
if(x>8||y>8||x<1||y<1)
return true;
if(x==ob_x&&y==ob_y)
return true;
if(visited[x][y])
return true;
return false;
}
int bfs(int x,int y){
int i;
queue<node> q;
node temp,newNode;
temp.x=x;
temp.y=y;
temp.step=0;
q.push(temp);
while(!q.empty()){
temp=q.front();
q.pop();
//返回步数
if(temp.x==result_x&&temp.y==result_y)
return temp.step;
for(i=0;i<8;i++){
newNode.x=temp.x+move[i][0];
newNode.y=temp.y+move[i][1];
if(check(newNode.x,newNode.y))
continue;
newNode.step=temp.step+1;
visited[newNode.x][newNode.y]=1;
q.push(newNode);
}

}

}
int main(){
int x,y;
int index=1;
while(~scanf("%d%d%d%d%d%d",&x,&y,&result_x,&result_y,&ob_x,&ob_y)){
memset(visited,0,sizeof(visited));
visited[x][y]=1;
int sum=bfs(x,y);
printf("Case %d: ",index);
index++;
printf("%d\n",sum);
}
return 0;

}


参考博客:http://blog.csdn.net/whjkm/article/details/44986067
http://blog.csdn.net/u012860063/article/details/41777745
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: