zoj 1091 Knight Moves

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;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注