蓝桥杯 九宫重排

本文探讨了蓝桥杯比赛中的九宫重排问题,结合C++编程语言,讲解如何解决经典的八数码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蓝桥杯 九宫重排

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

C++

#include <iostream>
#include <cstdio>
#include <string> 
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int factor[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
bool vis[400000]; 
struct Node {
	int x, y, st, pos, step;
	string s;
	Node() {}
	Node(int _x, int _y, int _pos) {
		x = _x;
		y = _y;
		pos = _pos;
	}
};

int cantor(string s) {
	int ans = 0;
	for(int i = 0; i < 8; i ++) {
		int num = 0;
		for(int j = i + 1; j <= 8; j ++) {
			if(s[j] < s[i]) num ++;
		}
		ans = ans + num * factor[8 - i];
	}
	return ans;
}

int bfs(string s, string end) {
	Node now, nxt;
	now.s = s;
	now.st = cantor(now.s);
	for(int i = 0; i < 3; i ++) {
		for(int j = 0; j < 3; j ++) {
			if(now.s[i * 3 + j] == '.') {
				now.x = i;
				now.y = j;
				now.pos = i * 3 + j;
			}
		}
	}
	now.step = 0;
	queue<Node> q;
	q.push(now);
	vis[now.st] = true;
	int ans = cantor(end);
	while(!q.empty()) {
		now = q.front();
		q.pop();
		if(ans == now.st) {
			return now.step;
		}
		for(int i = 0; i < 4; i ++) {
			int tx = now.x + dx[i];
			int ty = now.y + dy[i];
			int tpos = tx * 3 + ty;
			if(tx >= 0 && tx < 3 && ty >= 0 && ty < 3) {
				nxt.s = now.s;
				swap(nxt.s[now.pos], nxt.s[tpos]);
				nxt.st = cantor(nxt.s);
				if(!vis[nxt.st]) {
					nxt.x = tx;
					nxt.y = ty;
					nxt.pos = tpos;
					nxt.step = now.step + 1; 
					vis[nxt.st] = true;
					q.push(nxt);
				}
			}
		}
		
	}
	return -1;
}

int main()
{
	string a, b;
	cin >> a >> b;
	cout << bfs(a, b) << endl;
}

八数码问题

#include <iostream>
#include <cstdio>
#include <string> 
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int dx[] = {1, -1, 0, 0}; // x方向 
int dy[] = {0, 0, 1, -1}; // y方向 
// 阶乘 
int factor[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};
bool vis[400000]; // 标记 

struct Node{
	int x, y, st, pos, step;
	string s;
	Node() {}
	Node(int _x, int _y, int _pos) {
		x = _x;
		y = _y;
		pos = _pos;
	}
};

// 康托展开 
int cantor(string s) {
	int ans = 0;
	for(int i = 0; i < 8; i ++) {
		int num = 0;
		for(int j = i + 1; j <= 8; j ++){
			if(s[j] < s[i]) num ++;
		}
		ans = ans + num * factor[8 - i];
	}
	return ans;
}

// 宽度优先搜素 
int bfs(string s, string end) {
	Node now, nxt;
	now.s = s;
	now.st = cantor(now.s);
	for(int i = 0; i < 3; i ++) {
		for(int j = 0; j < 3; j ++) {
			if(now.s[i * 3 + j] == '0') {
				now.x = i;
				now.y = j;
				now.pos = i * 3 + j;
			}
		}
	}
	now.step = 0;
	queue<Node> q;
	q.push(now);
	vis[now.st] = true;
	int ans = cantor(end);
	while(!q.empty()) {
		now = q.front();
		q.pop();
		if(ans == now.st) {
			return now.step;
		}
		for(int i = 0; i < 4; i ++) {
			int tx = now.x + dx[i];
			int ty = now.y + dy[i];
			int tpos = tx * 3 + ty;
			if(tx >= 0 && tx < 3 && ty >= 0 && ty < 3) {
				nxt.s = now.s;
				swap(nxt.s[now.pos], nxt.s[tpos]);
				nxt.st = cantor(nxt.s);
				if(!vis[nxt.st]) {
					nxt.x = tx;
					nxt.y = ty;
					nxt.pos = tpos;
					nxt.step = now.step + 1; 
					vis[nxt.st] = true;
					q.push(nxt);
				}
			}
		}
		
	}
	return -1;
}

int main()
{
	string t;
	while(cin >> t) {
		string a, b;
		a += t;
		for(int i = 0; i < 2; i ++) cin >> t, a += t;
		cout << endl;
		for(int i = 0; i < 3; i ++) cin >> t, b += t;
		cout << endl;
		cout << bfs(a, b) << endl;
	}
}

/*
123
456
780
123
046
758

135
246
780
467
581
230

264
137
058
815
736
402
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值