hdu 1372(simple BFS)

博客提及这是一个基础的BFS问题,骑士移动方式奇特,若不知其移动规律,可能无法解决问题。若能从给定案例数据中找出问题规律,则很聪明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem link adress:click here

This is a basic BFS problem.But the way of knight's move  is strange,if you don't know the law of the knight's move,you are likely to cann't work the problem out.However,if you can find the regularity of the problem from the given cases's data,you must be very smart!

#include<iostream>
#include<queue>
#include<string>
using namespace std;
int map[10][10];
char s1[5],s2[5];
int visted[10][10];
int x1,y1;
int x2,y2;
int dir[8][2]={1,2,-1,2,1,-2,-1,-2,2,1,2,-1,-2,1,-2,-1};
struct node
{
	int x,y,step;
};
void BFS()
{
	int x,y;
	node cur,next;
	queue<node>q;
	memset(visted,0,sizeof(visted));
	cur.x=x1;
	cur.y=y1;
	cur.step=0;
	visted[x1][y1]=1;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(cur.x==x2&&cur.y==y2)
		{
			printf("To get from %s to %s takes %d knight moves.\n",s1,s2,cur.step);
			return ;
		}
		for(int i=0;i<8;i++)
		{
			next.x=x=cur.x+dir[i][0];
			next.y=y=cur.y+dir[i][1];
		    next.step=cur.step+1;
			if(x>=0&&x<8&&y>=0&&y<8)
			{
				visted[x][y]=1;
				q.push(next);
			}

		}
	}
}


int main()
{
	
	while(scanf("%s%s",s1,s2)!=EOF)
	{
		x1=s1[0]-'a';
		y1=s1[1]-'1';
		x2=s2[0]-'a';
		y2=s2[1]-'1';
		BFS();
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/heat-man/archive/2012/12/22/2829576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值