zoj 1091 KnightMoves

#include<stdio.h>
#include <queue>
using namespace std;
struct node { int x,y,cnt; }
int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};

 bool check(int x,  int y)
 {
    if(x >= 0 && x <=7 && y >= 0 && y <=7)
         return true;
     return false;
 }
int bfs(int sx,int sy,int ex,int ey)
{	bool visited[20][20]={false};
	queue<node> q;
	node cur={sx,sy,0};
	visited[sx][sy]=true;
	q.push(cur);
	 
	while(!q.empty())
	{
		
		cur=q.front();
		q.pop();
		if(cur.x==ex&&cur.y==ey)
		{
			return cur.cnt;
		}
		for(int i=0;i<7;i++)
		{ 
			int x=cur.x+dir[i][0];
			int y=cur.y+dir[i][1];
			if(check(x,y) && visited[x][y]==false)
			{	
				visited[x][y]=true;
				node tmp = {x, y, cur.cnt + 1};
				q.push(tmp);
			}
			
		}
	}
}
int main()
{
	char sx, sy, ex, ey;
    while(scanf("%c%c %c%c" ,&sx,&sy,&ex,&ey)!=EOF)
    {    getchar();
         sx -= 'a', sy = sy - '0' - 1;
         ex -= 'a', ey = ey - '0' - 1;//保持一致。 
        printf("To get from %c%d to %c%d takes %d knight moves.\n",
             sx + 'a', sy + 1, ex + 'a', ey + 1, BFS(sx, sy, ex,ey));
     }
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值