题意:给你8*8的棋盘和四个棋子的初始位置,每次可以向上下左右四个方向移动,若移动的点有一个棋子则可以跳过这个棋子移动,但一步不能跳两次。然后有4个目标状态,问能否在8步以内将棋子移动到目标状态。
题解:双向bfs,mark记录4个棋子4个坐标的状态。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char mark[8][8][8][8][8][8][8][8];
int dir[4][2]={
{0,1},
{0,-1},
{1,0},
{-1,0}
};
struct point
{
int x,y;
};
struct node
{
point p[4];
int step;
}s,e;
bool cmp(point a,point b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
int judge(node &k,int biao,int fx,int cishu)
{
if(cishu==0)
{
if(k.step>=4)return 0;
k.step++;
}
int dx=(k.p[biao].x+=dir[fx][0]);
int dy=(k.p[biao].y+=dir[fx][1]);
if(dx<=0||dy<=0||dx>8||dy>8)return 0;
for(int i=0;i<4;i++)
{
if(i==biao)continue;
if(dx==k.p