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

uva 439

时间:2015-09-09 11:17:28      阅读:129      评论:0      收藏: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;
	}
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

uva 439

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4793774.html

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