Solitaire HDU - 1401
https://vjudge.net/problem/HDU-1401
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.
There are four identical pieces on the board. In one move it is allowed to:
move a piece to an empty neighboring field (up, down, left or right),
jump over one neighboring piece to an empty field (up, down, left or right).
双向广搜,两边各搜四步,用
xhash[2][8][8][8][8][8][8][8][8]存储地图判重,但是会MLE
所以精细利用char0 - 255的每一个比特,规模减少了8倍:
#define set_bit(x,y) (x|=(0x01<<y)) //置位
#define clr_bit(x,y) (x&=(~(0x01<<y))) //清零
#define check_bit(x,y) (x&(0x01<<y)) //检测
所以就 过了
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug printf("第%d行\n",__LINE__)
#define print(a) cout<<#a<<": "<<a<<endl
#define prin2(a,b) cout<<#a<<": "<<(a)<<" "<<#b<<": "<<(b)<<endl;
#else
#define debug
#define print(name,a)
#define prin2(a,b)
#endif
#define set_bit(x,y) (x|=(0x01<<y)) //置位
#define clr_bit(x,y) (x&=(~(0x01<<y))) //清零
#define check_bit(x,y) (x&(0x01<<y)) //检测
namespace sxh
{
struct point
{
int x,y;//x,y 0 - 8.
};
struct sate
{
point a[4];
int step;
} s[2],tp; //start goal;
queue <sate> Q[2];
bool xmap[8][8];
char xhash[2][8][8][8][8][8][8][8];//xhash[0]start;
int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
void clear_set()
{
memset(xhash,0,sizeof(xhash));//清空数组
while(!Q[0].empty())//清空队列
{
Q[0].pop();
}
while(!Q[1].empty())
{
Q[1].pop();
}
}
bool cmp(point &a,point &b)
{
if(a.x == b.x)
{
return a.y < b.y;
}
else
{
return a.x > b.x;
}
}
void ssort(sate &s)
{
sort(s.a,s.a + 4,cmp);
}
void init()
{
for(int i = 1; i < 4; i++)
{
scanf("%d %d",&s[0].a[i].x,&s[0].a[i].y);
s[0].a[i].x--,s[0].a[i].y--;
}
for(int i = 0; i < 4; i++)
{
scanf("%d %d",&s[1].a[i].x,&s[1].a[i].y);
s[1].a[i].x--,s[1].a[i].y--;
}
}
void set_map(sate &s)
{
memset(xmap,0,sizeof(xmap));
for(int i = 0; i < 4; i++)
{
xmap[s.a[i].x][s.a[i].y] = 1;
}
}
void set_hash(bool b,sate &s)
{
set_bit(xhash[b][s.a[0].x][s.a[0].y][s.a[1].x][s.a[1].y][s.a[2].x][s.a[2].y][s.a[3].x],s.a[3].y);
}
bool get_hash(bool b,sate &s)
{
return check_bit(xhash[b][s.a[0].x][s.a[0].y][s.a[1].x][s.a[1].y][s.a[2].x][s.a[2].y][s.a[3].x],s.a[3].y);
}
bool check(int x,int y)
{
return (x>=0&&x<8&&y>=0&&y<8);
}
}
using namespace sxh;
bool tbfs()
{
set_hash(0,s[0]);
set_hash(1,s[1]);
s[0].step = 0;
s[1].step = 0;
Q[0].push(s[0]);
Q[1].push(s[1]);
while(!Q[1].empty()||!Q[0].empty())
{
while(!Q[0].empty())
{
sate stp = Q[0].front();Q[0].pop();
if(stp.step >= 4)continue;
set_map(stp);
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
sate p;
memcpy(&p,&stp,sizeof(sate));
p.a[i].x = stp.a[i].x + dir[j][0];
p.a[i].y = stp.a[i].y + dir[j][1];
if(!check(p.a[i].x,p.a[i].y))continue;
if(xmap[p.a[i].x][p.a[i].y])
{
p.a[i].x += dir[j][0];
p.a[i].y += dir[j][1];
if(!check(p.a[i].x,p.a[i].y))continue;
}
ssort(p);
if(get_hash(0,p))continue;
if(get_hash(1,p))return 1;
p.step = stp.step + 1;
set_hash(0,p);
Q[0].push(p);
}
}
}
if(!Q[1].empty())
{
sate stp = Q[1].front();Q[1].pop();
if(stp.step >= 4)continue;
set_map(stp);
for(int i = 0;i < 4;i++)
{
for(int j = 0;j < 4;j++)
{
sate p;
memcpy(&p,&stp,sizeof(sate));
p.a[i].x = stp.a[i].x + dir[j][0];
p.a[i].y = stp.a[i].y + dir[j][1];
if(!check(p.a[i].x,p.a[i].y))continue;
if(xmap[p.a[i].x][p.a[i].y])
{
p.a[i].x += dir[j][0];
p.a[i].y += dir[j][1];
if(!check(p.a[i].x,p.a[i].y))continue;
}
ssort(p);
if(get_hash(1,p))continue;
if(get_hash(0,p))return 1;
p.step = stp.step + 1;
set_hash(1,p);
Q[1].push(p);
}
}
}
}
return 0;
}
int main()
{
freopen("a.in","r",stdin);
while(~scanf("%d%d",&s[0].a[0].x,&s[0].a[0].y))
{
clear_set();
s[0].a[0].x--,s[0].a[0].y--;
init();
if(tbfs())
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
return 0;
}