时间限制:1秒 内存限制:128M
题目描述
在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”。有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字。他的同桌平时喜欢下围棋,知道这件事后觉得很有趣,就想试一试,在一个(100×100)的围棋盘上任选两点A、B,A点放上黑子,B点放上白子,代表两匹马。棋子可以按“日”字走,也可以按“田”字走,俩人一个走黑马,一个走白马。谁用最少的步数走到左上角坐标为(1,1)的点时,谁获胜。现在他请你帮忙,给你A、B两点的坐标,想知道两个位置到(1,1)点可能的最少步数。
提示:棋盘左上角坐标为(1,1),右下角坐标为(100,100)
输入描述
A、B两点的坐标。
输出描述
最少步数。
样例
输入
12 16 18 10
输出
8 9
#include<cmath>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=105;
const int M=10005;
int vis[N][N],step[N][N],f,r;
int q[M],q1[M];
int ax,ay,bx,by,as,bs;
int dx[]= {1,-1,2,-2,1,-1,2,-2,2,-2,-2,2};
int dy[]= {2,2,1,1,-2,-2,-1,-1,2,-2,2,-2};
void bfs(int x,int y) {
vis[x][y]=1;//起点标记入队
q[r] =x;
q1[r++] =y;
while(f<r) {
int tx=q[f] ;
int ty=q1[f] ;
if(tx==1&&ty==1){
cout<<step[tx][ty]<<endl;
}
for(int i=0; i<12; i++) {
int xx=tx+dx[i];
int yy=ty+dy[i];
if(xx<1||xx>100||yy<1||yy>100) {
continue;
}
if(vis[xx][yy]==0){
vis[xx][yy]=1;
q[r] =xx;
q1[r] =yy;
r++;
step[xx][yy]=step[tx][ty]+1;
}
}
f++;
}
}
int main() {
cin>>ax>>ay;
cin>>bx>>by;
bfs(ax,ay);
memset(vis,0,sizeof(vis));
memset(step,0,sizeof(step));
f=r=0;
bfs(bx,by);
return 0;
}
1291

被折叠的 条评论
为什么被折叠?



