zoj 1505 || poj 1198 Solitaire(状态压缩+双向BFS)

211 篇文章 0 订阅
53 篇文章 0 订阅

状态压缩+双向BFS。。。

我压了一个下午!!!!!!

呜呜。。><。。。

双向BFS理解的不好,我弄俩队列同时搜。。我错了。。。其实刚开始就想到了,这么搜万一有一个走6步,一个走2步,挺纠结的是把。

后来一直过不去,才知道。。。双BFS可以保存一个BFS的结果,然后第二个搜第一个的解。。。好神奇。。

我用了64位去压缩状态,其实感觉蛮鸡肋的,下午用了一下午的8位数,可能因为双BFS写得不好,一直TLE。。。

俩BFS结果都存,最后扫也可以,充分体现了,如果存一个,另一个去找,用引用,引用真的快了很多,如果没用引用,POJ的数据我都过不去。。囧。。

用八位数压缩的也过了~~在POJ比64位的跑得快。。。比在ZOJ的64位慢。。。囧。。

64bit:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)

using namespace std;

typedef long long LL;

void pro(LL &st)
{
	int x, y;
	scanf("%d%d", &x, &y);
	x--; y--;
	int p = x * 8 + y;
	st |= (1ll << p);	
}

queue< pair<LL,int> > q;
set<LL> sets, sett;
set<LL>::iterator it;
int dir[9] = {1,0, -1,0, 0,1, 0,-1};
bool Bin(LL st, int t, set<LL> &s, set<LL> &ss, bool f)
{
	bool a[100][100];
	int p[5][5], cnt = 0;
	memset(a, false, sizeof(a));
	FOR(i, 0, 64)
		if( st & (1ll << i) )
		{
			int x = i/8;
			int y = i%8;
			a[x][y] = true;
			p[cnt][0] = x;
			p[cnt++][1] = y;
		}
	FOR(i, 0, cnt)
	{
		LL tmp = st;
		tmp ^= (1ll << (p[i][0]*8 + p[i][1]));
		LL la = tmp;
		for(int k=0; k<8; k+=2)
		{
			tmp = la;
			int x = p[i][0] + dir[k];
			int y = p[i][1] + dir[k+1];
			if( x >= 0 && x < 8 && y >= 0 && y < 8 )
				if( !a[x][y] )
				{
					tmp ^= (1ll << (x * 8 + y));
					if( !s.count(tmp) )
					{
						if( f && ss.count(tmp) ) return true;
						q.push(make_pair(tmp, t));
						s.insert(tmp);
					}
				}
				else
				{
					x += dir[k];
					y += dir[k+1];	
					if( x >= 0 && x < 8 && y >= 0 && y < 8 && !a[x][y] )
					{
						tmp ^= (1ll << (x * 8 + y));
						if( !s.count(tmp) )
						{
							if( f && ss.count(tmp) ) return true;
							q.push(make_pair(tmp, t));
							s.insert(tmp);
						}
					}
				}
		}
	}
	return false;
}

bool BFS(LL st, set<LL> &s, set<LL> &ss, bool f)
{
	s.clear();
	while( !q.empty() ) q.pop();
	s.insert(st);
	q.push(make_pair(st, 0));
	
	while( !q.empty() )
	{
		if( q.front().second < 4 )
		{
			bool ans = Bin(q.front().first, q.front().second + 1, s, ss, f);
			if( ans ) return true;
		}
		else
			return false;
		q.pop();
	}
	return false;
}
	
bool solve(LL st, LL tt)
{
	BFS(st, sets, sett, false);
	return BFS(tt, sett, sets, true);
}

int main()
{
	LL st, tt;
	int x, y;
	
	while( ~scanf("%d%d", &x, &y) )
	{
		st = tt = 0;
		x--; y--;
		int p = x * 8 + y;
		st |= (1ll << p);
		
		FOR(i, 0, 3) pro( st );
		FOR(i, 0, 4) pro( tt );
		
		bool ans = solve(st, tt);
		
		puts(ans ? "YES" : "NO");
	}

return 0;
}

8位数:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)

using namespace std;

typedef pair<int,int> pii;
typedef set<int> mpp;
int p[5];
void pro(int i)
{
	int x, y;
	scanf("%d%d", &x, &y);
	p[i] = x*10 + y;
}
queue<pii> q;
mpp sets, sett;
bool cmp(int a,int b)
{
	return a < b;
}
int ST(int *tmp)
{
	int st = 0;
	int p[5];
	memcpy(p, tmp, sizeof(int)*5);
	sort(p, p+4);
	FOR(i, 0, 4)
	{
		st *= 100;
		st += p[i];
	}
	return st;
}
int dir[4] = {1, -1, 10, -10};
bool AI(int st, int t, mpp &s, mpp &ss, bool f)
{
	if( !s.count(st) )
	{
		if( f && ss.count(st) ) return true;
		q.push(make_pair(st, t));
		s.insert(st);
	}
	return false;	
}

bool Bin(int st, int t, mpp &s, mpp &ss, bool f)
{
	bool a[100];
	memset(a, false, sizeof(a));
	FOR(i, 0, 4)
	{
		int x = st % 100;
		st /= 100;
		a[x] = true;
		p[i] = x;
	}
	
	int tmp[5];
	memcpy(tmp, p, sizeof(p));
	FOR(i, 0, 4)
	{
		int x = p[i];
		FOR(k, 0, 4)
		{
			int xx = x + dir[k];
			int aa = xx / 10;
			int bb = xx % 10;
			if( aa >= 1 && aa <= 8 && bb >= 1 && bb <= 8 )
				if( !a[xx] )
				{
					tmp[i] = xx;
					int la = ST( tmp );
					bool res = AI( la, t+1, s, ss, f);
					if( res ) return true;
				}
				else
				{
					xx += dir[k];
					aa = xx / 10;
					bb = xx % 10;	
					if( aa <= 8 && aa >= 1 && bb <= 8 && bb >= 1 && !a[xx] )
						{
							tmp[i] = xx;
							int la = ST( tmp );
							bool res = AI( la, t+1, s, ss, f );
							if( res ) return true;
						}
				}
		}
		tmp[i] = p[i];
	}
	return false;
}

bool BFS(int st, mpp &s, mpp &ss, bool f)
{
	s.clear();
	while( !q.empty() ) q.pop();
	s.insert(st);
	q.push(make_pair(st, 0));
	
	while( !q.empty() )
	{
		if( q.front().second < 4 )
		{
			bool ans = Bin(q.front().first, q.front().second, s, ss, f);
			if( ans ) return true;
		}
		q.pop();
	}
	return false;
}

bool solve(int st, int tt)
{
	if( st == tt ) return true;

	BFS(st, sets, sett, false);
	return BFS(tt, sett, sets, true);
}
	
int main()
{
	int st, tt;
	int x, y;
	while( ~scanf("%d%d", &x, &y) )
	{
		p[0] = x * 10 + y;

		FOR(i, 1, 4) pro( i );
		st = ST( p );
		
		FOR(i, 0, 4) pro( i );
		tt = ST( p );
		
		bool ans = solve(st, tt);
		
		puts(ans ? "YES" : "NO");
	}

return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值