描述
在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”。有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字。
他的同桌平时喜欢下围棋,知道这件事后觉得很有趣,就想试一试,在一个(100×100)的围棋盘上任选两点A、B,A点放上黑子,B点放上白子,代表两匹马。棋子可以按“日”字走,也可以按“田”字走,俩人一个走黑马,一个走白马。谁用最少的步数走到左上角坐标为(1,1)的点时,谁获胜。现在他请你帮忙,给你A、B两点的坐标,想知道两个位置到(1,1)点可能的最少步数。
格式
输入格式
A、B两点的坐标。
输出格式
最少步数。
样例
输入样例
12 16
18 10
输出样例
8
9
限制
时间限制: 1000 ms
内存限制: 65536 KB
十二联通问题
int dx[12]={1,1,-1,-1,2,2,-2,-2,2,2,-2,-2};
int dy[12]={2,-2,2,-2,1,-1,1,-1,2,-2,-2,2};
//马八个方向,象四个方向
struct node{
int x,y,step;
}a,b,temp;
int mp[110][110];
bool judge(int x,int y){
if(x<0||x>100||y<0||y>100) return false;
if(mp[x][y]==1) return false;
return true;
}
int bfs(node noode){
queue<node> q;
q.push(noode);
while(!q.empty()){
node top=q.front();
q.pop();
if(top.x==1&&top.y==1){
return top.step;
}
for(int i=0;i<12;i++){
int newx=top.x+dx[i];
int newy=top.y+dy[i];
if(judge(newx,newy)){
temp.x=newx;
temp.y=newy;
temp.step=top.step+1;
q.push(temp);
mp[newx][newy]=1;
}
}
}
}
int main(){
cin>>a.x>>a.y;
a.step=0,b.step=0;
cout<<bfs(a)<<'\n';
memset(mp,0,sizeof(mp));
cin>>b.x>>b.y;
cout<<bfs(b)<<'\n';
return 0;
}