挑战程序设计竞赛 2章习题 AOJ 0121 Seven Puzzle bfs

地址  https://vjudge.net/problem/Aizu-0121

题目大意是 0~7 8个数字随机分布在两行中,我们可以上下左右交换0和它周边的数字来将8个数字组成最终形态

如图

最终形态

输入格式

一行输入0~7   8个数字  空格隔开

输出格式

每行输出上述数据 达到最终形态的步数

Sample Input
0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
Output for the Sample Input
0
1
28

解法:

使用bfs得到最短移动步骤

但是由于没有明确的移动目标,我们不清楚如何移动才是与最终形态接近了,所以可以逆向思考,从最终形态生成所有可能的组合和达到这个组合的步数,使用哈希记录方便查询。

之后直接查询即可。

进阶的资料 随着状态数目的增加比如八数码 需要考虑状态压缩。 一个状态是否可以达到最终形态,可以使用逆序对来确认,关键字是康托展开,大家可以自行查询。

代码

复制代码
// 11235555.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>

using namespace std;

string targetStr = "01234567";

unordered_map<string, int> mm;

int mv[4] = { 1,-1,4,-4 };

void bfs() {
    queue<pair<string, int>> q;
    q.push({ targetStr ,0 });

    while (!q.empty()) {
        pair<string, int> p = q.front(); q.pop();
        string  s = p.first; int step = p.second;
        mm[s] = step;

        int idx = s.find('0');
        for (int i = 0; i < 2; i++) {
            int newidx = idx + mv[i];
            if (idx / 4 == newidx / 4 && newidx >=0 && newidx<8) {
                string tmp = s;  swap(tmp[idx], tmp[newidx]);
                if (mm.count(tmp) == 0) {
                    mm[tmp] = step + 1;
                    q.push({ tmp,step + 1 });
                }
            }
        }
        for (int i = 2; i < 4; i++) {
            int newidx = idx + mv[i];
            if (idx / 4 != newidx / 4 && newidx >= 0 && newidx < 8) {
                string tmp = s;  swap(tmp[idx], tmp[newidx]);
                if (mm.count(tmp) == 0) {
                    mm[tmp] = step + 1;
                    q.push({ tmp,step + 1 });
                }
            }
        }
    }



}

int main()
{
    bfs();
    string s;
    while (getline(cin,s)) {
        string curr;
        for (auto& e : s) {
            if (e != ' ') curr += e;
        }
        cout << mm[curr] <<endl;
    }

    return 0;
}
复制代码

 

posted on   itdef  阅读(119)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2017-01-17 网站架设学习笔记

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示