#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;
}
}
最少步数
最新推荐文章于 2024-06-17 22:13:12 发布