八数码问题

  • 利用哈希表来查重
  • 广度优先来取得最少步数
  • 参考再论八数码文章对八数码问题有无解的讨论
  • 下面是八数码实现代码
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

#define M 1000000
typedef int state[9];
int st[M][9];
int goal[9];
int dist[M];
//set<int> vis;
const int MAX = 1000003;
int head[MAX], nxt[M];

const int dxy[4][2] = { 0, -1,
						-1, 0,						 
						0, 1, 
						1, 0 };

int try_to_insert(int rear)
{
	int value = 0;
	state& s = st[rear];
	for (int i = 0; i < 9; i++)
		value = value * 10 + s[i];
	/*if (vis.find(value) != vis.end())	return 0;
	vis.insert(value);
	return 1;*/
	int h = value % MAX;
	int u = head[h];
	while (u)
	{
		if (memcmp(st[u], s, sizeof(s)) == 0) return 0;
		u = nxt[u];
	}
	nxt[rear] = head[h];
	head[h] = rear;
	return 1;
}

int bfs()
{
	int front = 1, rear = 2;
	while (front < rear)
	{
		state& s = st[front];
		if (memcmp(s, goal, sizeof(s)) == 0)	return front;
		int z;
		for (z = 0; z < 9; z++) if (!s[z])	break;
		for (int d = 0; d < 4; d++)
		{
			int newx = dxy[d][0] + z / 3;
			int newy = dxy[d][1] + z % 3;
			int newz = newx * 3 + newy;
			if (newx >= 0 && newx < 3 && newy >= 0 && newy < 3)
			{
				state& t = st[rear];
				memcpy(t, s, sizeof(s));
				t[newz] = s[z];
				t[z] = s[newz];
				if (memcmp(t, goal, sizeof(s)) == 0)	{ dist[rear] = dist[front] + 1; return rear; }
				if (try_to_insert(rear))
				{
					dist[rear] = dist[front] + 1;
					rear++;
				}
			}
		}
		front++;
	}
	return 0;
}

int main()
{
	//while (1)
	//{
		for (int i = 0; i < 9; i++)	cin >> st[1][i];
		for (int i = 0; i < 9; i++) cin >> goal[i];
		int ans = bfs();
		if (ans)	cout << dist[ans] << endl;
		else
		{
			cout << -1 << endl;
		}
		system("pause");
	//}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值