uva 439

3 篇文章 0 订阅
/*************************************************************************
     File Name: uva439.cpp
     Author: yubo
     Mail: yuzibode@126.com 
     Created Time: 2014/5/18 23:15:55
reference:http://blog.sina.com.cn/s/blog_6730a3aa0100vkzd.html
类似于中国象棋中的马的走法,但是这道题改来改去终于自己做出来了
input:
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
 ************************************************************************/

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int dir[8][2]={
	{2,1},{2,-1},{1,2},{1,-2},{-1,2},{-1,-2},{-2,1},{-2,-1},
};
typedef struct{
	int x,y;
	int times;

}SE;
int x1,y1,x2,y2;//
int judge1(int i,int j)
{
	if(i>=0&&i<8&&j>=0&&j<8)
		return 1;
	else
		return 0;

}
int vis[8][8];
char command[2][5];//记录命令
int ans;
void bfs(int x,int y)
{
	queue<SE> q;
	SE q0,q1,q2;
	q0.x=x;
	q0.y=y;
	q0.times=0;
	vis[x][y]=1;
	q.push(q0);
	while(!q.empty()){
		q1=q.front();
		q.pop();
		for(int i=0;i<8;i++){
			int temp1=q1.x+dir[i][0];
			int temp2=q1.y+dir[i][1];
			if(judge1(temp1,temp2)&&!vis[temp1][temp2]){
				if(temp1==x2&&temp2==y2){
					ans=q1.times+1;
					return ;
				}
				q2.x=temp1;
				q2.y=temp2;
				q2.times=q1.times+1;
				vis[temp1][temp2]=1;
				q.push(q2);
			}
		}


	}


}
int main()
{
//	freopen("in.txt","r",stdin);
	while(cin>>command[0]>>command[1]){
		memset(vis,0,sizeof(vis));
		y1=command[0][0]-'a';
		x1=command[0][1]-'1';
		y2=command[1][0]-'a';
		x2=command[1][1]-'1';
		ans=0;
//		printf("%d %d %d %d\n",x1,y1,x2,y2);
//		printf("%s\n",command[0]);
		bfs(x1,y1);
		cout<<"To get from "<<command[0]<<" to "<<command[1]<<" takes "<<ans<<" knight moves."<<endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值