c++解决八数码问题

在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局,找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。

八数码问题是典型的bfs问题,bfs问题经常用来解决最短路径问题。

输入案例;:

1 2 3 0 8 4 7 6

1 0 3  2 4 7 6 5

输出样例:

2

#include<iostream>
#include<queue>
using namespace std;
int dir[4][2] = { {-1,0},{0,-1},{1,0},{0,1} };
const int len = 362880;
int start[9];
int goal[9];
struct node
{
	int state[9];
	int dis;
};
int visited[len] = { 0 };//判重数组
long int factory[] = { 1,1,2,6,24,120,720,5040,40320,362880 };
//判重函数,康托展开。
bool Cantor(int str[], int n) {
	long result = 0;
	for (int i = 0; i < n; i++) {
		int count = 0;
		for (int j = i + 1; j < n; j++) {
			if (str[i] > str[j]) {
				count++;
			}
		}
		result += count * factory[n - 1 - i];
	}
	if (!visited[result]) {
		visited[result] = 1;
		return 1;
	}
	else
		return 0;
}
int bfs() {
	node head;
	memcpy(head.state, start, sizeof(head.state));
	head.dis = 0;
	Cantor(head.state, 9);
	queue<node> q;
	q.push(head);
	while (!q.empty()) {
		head= q.front();
		if (memcmp(head.state, goal, sizeof(goal)) == 0) {
			return head.dis;
		}
		q.pop();
		int z;
		//找到此时的0的位置
		for (z = 0; z < 9; z++) {
			if (head.state[z] == 0)
				break;
		}
		//转化为二维坐标
		int x = z % 3;
		int y = z / 3;
		for (int i = 0; i < 4; i++) {
			int dx = x + dir[i][0];
			int dy = y + dir[0][i];
			int nz = 3 * dy + dx;
			if (dx >= 0 && dx < 3 && dy >= 0 && dy < 3) {
				node n1;
				memcpy(&n1, &head, sizeof(node));
				swap(n1.state[z], n1.state[nz]);
				n1.dis++;
				if (Cantor(n1.state, 9)) {
					q.push(n1);
				}
			}
		}

	}
	return -1;
}
int main() {
	for (int i = 0; i < 9; i++) 
		cin >> start[i];
	for (int j = 0; j < 9; j++) 
		cin >> goal[j];
	int num = bfs();
	if (num != -1) {
		cout << num << endl;
	}
	else
		cout << "impossible" << endl;
	return 0;
}

函数讲解

memcpy()

描述:将指定字符串复制到目标字符串,此函数有个数限制,与函数-strncpy用法差不多。

格式:memcpy(aim,str,n)。

swap()

在main函数中创建两个值,通过调用函数对其进行交换。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用A*算法解决八数码问题C++代码示例: ```c++ #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXN = 1e5 + 5; const int INF = 0x3f3f3f3f; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; struct node { int x, y, step, f; int a[3][3]; bool operator < (const node &rhs) const { return f > rhs.f; } }; int n, m, sx, sy, ex, ey; int vis[MAXN], ans[MAXN]; node st, ed; int get_h(node a, node b) { int res = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (a.a[i][j] == 0) continue; for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { if (a.a[i][j] == b.a[k][l]) { res += abs(i - k) + abs(j - l); } } } } } return res; } bool check(node a) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (a.a[i][j] != ed.a[i][j]) { return false; } } } return true; } void A_star() { priority_queue<node> q; st.f = get_h(st, ed); q.push(st); while (!q.empty()) { node u = q.top(); q.pop(); if (check(u)) { printf("%d\n", u.step); return; } for (int i = 0; i < 4; i++) { int nx = u.x + dx[i]; int ny = u.y + dy[i]; if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue; node v = u; swap(v.a[u.x][u.y], v.a[nx][ny]); v.x = nx, v.y = ny; v.step++; v.f = v.step + get_h(v, ed); if (vis[v.a[0][0] * 1000000 + v.a[0][1] * 100000 + v.a[0][2] * 10000 + v.a[1][0] * 1000 + v.a[1][1] * 100 + v.a[1][2] * 10 + v.a[2][0]] == 0) { vis[v.a[0][0] * 1000000 + v.a[0][1] * 100000 + v.a[0][2] * 10000 + v.a[1][0] * 1000 + v.a[1][1] * 100 + v.a[1][2] * 10 + v.a[2][0]] = 1; q.push(v); } } } } int main() { scanf("%d%d", &sx, &sy); st.x = sx, st.y = sy; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &st.a[i][j]); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &ed.a[i][j]); } } A_star(); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值