zoj 1091 Knight Moves

/*
继续广搜
一次AC
呵呵。。。
*/
#define LOCAL
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<iomanip>
#include<string>
#include<algorithm>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
class Cordinate
{public:int x,y;};
using namespace std;
int dir[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1},chess[10][10];
bool legal(Cordinate po)
{if(po.x>=1&&po.x<=8&&po.y>=1&&po.y<=8)return true;return false;}
int main()
{
#ifdef LOCAL
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
#endif

	   char a[3],b[3];Cordinate po,pos;int i;
	   while(cin>>a>>b)
	   {
		    queue<Cordinate>q;
		    memset(chess,-1,sizeof(chess));
		    pos.x=a[0]-'a'+1;pos.y=a[1]-'0';
			chess[pos.x][pos.y]=0;
			q.push(pos);
			while(!q.empty())
			{
				po=q.front();q.pop();
				for(i=0;i<8;i++)
				{
					pos.x=dir[i][0]+po.x;pos.y=dir[i][1]+po.y;
					if(legal(pos)&&chess[pos.x][pos.y]==-1) {q.push(pos);chess[pos.x][pos.y]=chess[po.x][po.y]+1;}
				}
			}
			cout<<"To get from "<<a<<" to "<<b<<" takes "<<chess[b[0]-'a'+1][b[1]-'0']<<" knight moves."<<endl;
	   }
		return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值