最少步数

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int dir[12][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2},{-2,-2},{-2,2},{2,-2},{2,2}};
struct pace{
	int x,y,step;
};
int vis[101][101],cnt;
bool check(int x,int y)
{
	if(x<=100&&y<=100&&x>0&&y>0&&!vis[x][y])return true;
	return false;
}
queue<pace>q;
void bfs(int x,int y)
{
	while(q.size())q.pop();//不清空队列会影响下一组的数据
	pace cur;
	cur.x=x;
	cur.y=y;
	cur.step=0;
	q.push(cur);
	while(q.size())
	{
	
		pace now=q.front();
		if(now.x==1&&now.y==1)
		{
			cnt=now.step;
			return;
		}
		q.pop();
		for(int i=0;i<12;i++)
		{
			int nx=now.x+dir[i][0];
			int ny=now.y+dir[i][1];
			if(check(nx,ny))
			{
				vis[nx][ny]=1;
				pace next;
				next.x=nx;
				next.y=ny;
				next.step=now.step+1;
				q.push(next);
			}
		}
	}
}
int main()
{
		int A,B;
		while(scanf("%d%d",&A,&B)!=EOF)
		{
			memset(vis,0,sizeof(vis));
		    vis[A][B]=1;
		  	bfs(A,B);
			cout<<cnt<<endl;
		}
		
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值