搜索 HOJ 1868 八数码

八数码

My Tags   (Edit)
  Source : HCPC 2005 Spring
  Time limit : 2 sec   Memory limit : 32 M

Submitted : 1434, Accepted : 399

在 3*3 的棋盘上有 8 个将牌,每一个将牌都刻有 1-8 数码中的某一个数码。棋盘中留有一个空格,允许其周围的某一个将牌向空格移动,这样通过移动将牌就可以不断改变将牌的布局。

给定一种初始时的将牌布局或结构(称作初始状态)和一个目标的布局(称作目标状态),问如何移动将牌,实现从初始状态到目标状态的改变,给出合法步骤中最小的移动步数。如下图所示

搜索 HOJ 1868 八数码 - 恶魔仁 - 恶魔仁

输入格式:首先输入一个数字 t (t>=1 && t<=10) 为测试数据的总数,接下来是 t 组测试数据, 每组测试数据均由 6 行,每行 3 个数字组成,前 3 行为初始状态,后 3 行为目标状态,空格由 0 表示,每组测试数据前面有一个空行。

输出格式:给出可以使初始状态转换到目标状态的最小移动次数,如果转化不到打印出-1。

输入样例:

2

2 8 3
1 6 4
7 0 5
2 3 0
1 8 4
7 6 5

2 8 3
1 6 4
7 0 5
2 8 3
1 4 6
7 0 5

输出样例:

3
-1

题意:给出一个八数码的初始状态,给出结束状态,问从初始状态到结束状态最少需要移动多少步,如果不可达,输出-1

思路:双广搜索。。。。判断不可达的话可以用逆序数,如果两个状态同为奇排列或者偶排列,那么可达,否则不可达。然后hash用的是康托展开。。。。

代码:
#include <vector>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <string>
#include <iostream>
#include<cassert>
#include<queue>
#include <algorithm>
#include <cassert>
#include <math.h>
using namespace std;
#define eps 1e-8
#define LL long long
const int maxn =370000;
int factorial[10];

int Move[2][4] = { { 0 , 1 , 0 , -1 } , { 1 , 0 , -1 , 0 } };
int d[2][maxn];

int hash(char state[3][3])
{
	int ret = 0;
	char *p = &state[0][0];
	for (int i = 0 ; i < 9 ; ++i)
	{
		int cnt = 0;
		for (int j = i+1 ; j < 9 ; ++j) if (p[i] > p[j])
			++cnt;
		ret += factorial[8-i]*cnt;
	}
	return ret;
}

struct Node
{
	int r , c;
	char state[3][3];
	int hashcode;
}now ,init , dest;

void input()
{
	for (int i = 0 ; i < 3 ; ++i)
		scanf(" %c %c %c%*c",&init.state[i][0],&init.state[i][1],&init.state[i][2]);
	for (int i = 0 ; i < 3 ; ++i)
	{
		for (int j = 0 ; j < 3 ; ++j) if (init.state[i][j]=='0')
		{
			init.r = i;
			init.c = j;
			init.hashcode = ::hash(init.state);
			break;
		}
	}
	for (int i = 0 ; i < 3 ; ++i)
		scanf(" %c %c %c%*c",&dest.state[i][0],&dest.state[i][1],&dest.state[i][2]);
	for (int i = 0 ; i < 3 ; ++i)
	{
		for (int j = 0 ; j < 3 ; ++j) if (dest.state[i][j]=='0')
		{
			dest.r = i;
			dest.c = j;
			dest.hashcode = ::hash(dest.state);
			break;
		}
	}
}

bool solvable()
{
	char *p = &init.state[0][0];
	int cnt1 = 0;
	for (int i = 0 ; i < 9 ; ++i)
	{
		for (int j = i+1 ; j < 9 ; ++j) if (p[j]!='0' && p[i] > p[j])
			++cnt1;
	}
	p = &dest.state[0][0];
	int cnt2 = 0;
	for (int i = 0 ; i < 9 ; ++i)
	{
		for (int j = i+1 ; j < 9 ; ++j) if (p[j]!='0' && p[i] > p[j])
			++cnt2;
	}
	if ( (cnt1%2) == (cnt2%2) ) return true;
	return false;
}

int Dbfs()
{
	if (init.hashcode==dest.hashcode) return 0;
	memset(d,-1,sizeof(d));
	queue<Node> q[2];
	int hashcode = ::hash(init.state);
	q[0].push(init);
	d[0][hashcode] = 0;
	hashcode = ::hash(dest.state);
	q[1].push(dest);
	d[1][hashcode] = 0;
	int cur = 0;
	while (q[cur].size() || q[cur].size())
	{
		if (!(q[cur].size()!=0 && q[cur].size()<=q[!cur].size()))
			cur = !cur;
		now = q[cur].front(); q[cur].pop();
		int r = now.r;
		int c = now.c;
		Node tmp = now;
		for (int i = 0 ; i < 4 ; ++i)
		{
			now = tmp;
			int rr = r + Move[0][i];
			int cc = c + Move[1][i];
			if (rr < 0 || rr > 2 || cc < 0 || cc > 2) continue;
			swap(now.state[rr][cc],now.state[r][c]);
			int hashcode = ::hash(now.state);
			if (d[!cur][hashcode]!=-1) return 1+d[cur][now.hashcode]+d[!cur][hashcode];
			if (d[cur][hashcode]!=-1) continue;
			d[cur][hashcode] = d[cur][now.hashcode] + 1;
			now.hashcode = hashcode;
			now.r = rr;
			now.c = cc;
			q[cur].push(now);
		}
	}
}


int main()
{
	factorial[0] = factorial[1] = 1;
	for (int i = 2 ; i <= 9 ; ++i) 
		factorial[i] = factorial[i-1]*i;
	int T;
	scanf("%d ",&T);
	while (T--)
	{
		input();
		if (!solvable()) 
		{
			printf("-1\n");
			continue;
		}
		printf("%d\n",Dbfs());
	}
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值