【题目描述】
在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”。有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字。他的同桌平时喜欢下围棋,知道这件事后觉得很有趣,就想试一试,在一个(100×100)的围棋盘上任选两点A、B,A点放上黑子,B点放上白子,代表两匹马。棋子可以按“日”字走,也可以按“田”字走,俩人一个走黑马,一个走白马。谁用最少的步数走到左上角坐标为(1,1)的点时,谁获胜。现在他请你帮忙,给你A、B两点的坐标,想知道两个位置到(1,1)点可能的最少步数。
【输入】
A、B两点的坐标。
【输出】
最少步数。
【输入样例】
12 16
18 10
【输出样例】
8
9
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int m,n,step;
int a[100][100];
int vis[100][100];
int h[13][2]={0,0,-2,-1,-2,-2,-1,-2,1,-2,2,-2,2,-1,2,1,2,2,1,2,-1,2,-2,2,-2,1};
struct node
{
int x,y;
}s;
void bfs(int x,int y)
{
queue<node> q;
s.x=x,s.y=y;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
x=s.x,y=s.y;
for(int i=1;i<=12;i++)
{
int hx=x+h[i][0];
int hy=y+h[i][1];
if(hx>=1&&hx<=100&&hy>=1&&hy<=100&&!vis[hx][hy])
{
s.x=hx,s.y=hy;
q.push(s);
a[hx][hy]=a[x][y]+1;
vis[hx][hy]=1;
if(hx==1&&hy==1)
{
step=a[hx][hy];
return;
}
}
}
}
}
int main()
{
while(cin>>m>>n&&(m!=1||n!=1))
{
step=0;
memset(vis,0,sizeof(vis));
memset(a,0,sizeof(a));
bfs(m,n);
cout<<step<<endl;
}
return 0;
}