题目大意:有两匹马,马可以走"日",也可以像象走"田",求它走到(1,1)的步数。
题解:bfs
卡点:边界判断成了可以走到(0,y)或(x,0)
C++ Code:
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=51;
int dx[12]={2,-2,2,-2,1,1,-1,-1,2,2,-2,-2};
int dy[12]={2,2,-2,-2,2,-2,2,-2,1,-1,1,-1};
int sx,sy,h,t;
int q[maxn*maxn+100][2],s[maxn][maxn];
bool cj(int x,int y){
if (x<1||y<1||x>50||y>50)return true;
if (s[x][y])return true;
return false;
}
int bfs(int sx,int sy){
if (sx==1&&sy==1)return 0;
s[sx][sy]=1;
q[t=1][h=0]=sx;q[1][1]=sy;
while (h<t){
int x=q[++h][0],y=q[h][1];
for (int i=0;i<12;i++){
int xx=x+dx[i],yy=y+dy[i];
if (cj(xx,yy))continue;
q[++t][0]=xx;q[t][1]=yy;
s[xx][yy]=s[x][y]+1;
if (xx==1&&yy==1)return s[1][1]-1;
}
}
return 0;
}
int main(){
scanf("%d%d",&sx,&sy);
printf("%d\n",bfs(sx,sy));
memset(s,0,sizeof(s));
scanf("%d%d",&sx,&sy);
printf("%d\n",bfs(sx,sy));
return 0;
}