码迷,mamicode.com
首页 > 其他好文 > 详细

CSU 残缺的棋盘 (BFS)

时间:2015-08-25 16:53:11      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

Description

技术分享

Input

输入包含不超过10000 组数据。每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同。

Output

对于每组数据,输出测试点编号和最少步数。

Sample Input

<span class="sampledata">1 1 8 7 5 6
1 1 3 3 2 2</span>

Sample Output

<span class="sampledata">Case 1: 7
Case 2: 3</span>
<span class="sampledata"></span><pre name="code" class="html">#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int di,dj,r,c;
int m[9][9];
struct node
{
	int i,j,time;
};
queue<node>qq;
int x[8]={0,1,1,1,0,-1,-1,-1},y[8]={1,1,0,-1,-1,-1,0,1};

void bfs()
{
	while(!qq.empty()) {
		node temp=qq.front();
		node t=temp;
		qq.pop();
		if(t.i==r&&t.j==c) break;
		for(int i=0;i<8;i++) {
			t=temp;
			t.i+=x[i];
			t.j+=y[i];
			t.time+=1;
			if(t.i<1 || t.i>8 ||t.j<1 || t.j>8) continue;
			if(t.i==r && t.j==c) continue;
			if(t.time<m[t.i][t.j]) {
				m[t.i][t.j]=t.time;
				qq.push(t);
			}
		}
	}
}


int main()
{
	int si,sj,i,j,sigh;
	sigh=0;
	while(cin>>si>>sj>>di>>dj>>r>>c) {
		for(i=1;i<9;i++) {
			for(j=1;j<9;j++)
			m[i][j]=10000;
		}
		m[si][sj]=0;
		while(!qq.empty()) qq.pop();
		node temp={si,sj,0};
		qq.push(temp);
		bfs();
		printf("Case %d: %d\n",++sigh,m[di][dj]);
	}
}



<span class="sampledata">
</span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

CSU 残缺的棋盘 (BFS)

标签:

原文地址:http://blog.csdn.net/h1021456873/article/details/47976099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!