八数码问题

利用启发式搜索,找出以下问题的最优解。

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; const int BLANK = 0;
const int R = 3;
const int C = 3; pair<int, int> find_blank(const vector<vector<int>> &m) {
for (int r = 0; r < m.size(); r++) {
for (int c = 0; c < m[r].size(); c++) {
if (m[r][c] == BLANK) {
return make_pair(r, c);
}
}
}
return make_pair(-1, -1);
} vector<pair<int, int>> address_derive(const pair<int, int> &father) {
vector<pair<int, int>> addresses;
const int r = father.first, c = father.second;
if (r - 1 >= 0) {
addresses.push_back(make_pair(r - 1, c));
}
if (r + 1 < R) {
addresses.push_back(make_pair(r + 1, c));
}
if (c - 1 >= 0) {
addresses.push_back(make_pair(r, c - 1));
}
if (c + 1 < C) {
addresses.push_back(make_pair(r, c + 1));
}
return addresses;
} vector<vector<vector<int>>> matrix_derive(const vector<vector<int>> &father) {
vector<vector<vector<int>>> matrices;
pair<int, int> address = find_blank(father);
vector<pair<int, int>> addresses = address_derive(address);
for (const auto e : addresses) {
int r = e.first, c = e.second;
vector<vector<int>> son = father;
swap(son[address.first][address.second], son[r][c]);
matrices.push_back(son);
}
return matrices;
} int evaluate(const vector<vector<int>> &m, const vector<vector<int>> &goal) {
int difference = 0;
for (int r = 0; r < goal.size(); r++) {
for (int c = 0; c < goal[r].size(); c++) {
if (goal[r][c] == BLANK) {
continue;
}
difference = m[r][c] != goal[r][c] ? difference + 1 : difference;
}
}
return difference;
} struct Block {
int g;
int h;
vector<vector<int>> m;
Block(int g = 0, int h = 0, vector<vector<int>> m = vector<vector<int>>())
:g(g), h(h), m(m) {};
}; void print(const Block &b) {
for (const auto r : b.m) {
for (const auto c : r) {
cout << c << " ";
}
cout << endl;
}
cout << b.g << " " << b.h << endl;
cout << endl;
} bool in_close(const vector<vector<vector<int>>> &close, const vector<vector<int>> &m) {
for (auto e : close) {
if (e == m) {
return true;
}
}
return false;
} bool in_open(const vector<Block> &open, const vector<vector<int>> &m) {
for (auto e : open) {
if (e.m == m) {
return true;
}
}
return false;
} void search(const vector<vector<int>> &begin, const vector<vector<int>> &goal) {
vector<Block> open;
vector<vector<vector<int>>> close;
open.push_back(Block(0, evaluate(begin, goal), begin));
int g = 0;
while (open.size()){
sort(open.begin(), open.end(), [](const Block &lhs, const Block &rhs) {
return (lhs.g + lhs.h) < (rhs.g + rhs.h);
}); Block cur = open.front();
open.erase(open.begin());
close.push_back(cur.m); print(cur); if (cur.m == goal) {
break;
} vector<vector<vector<int>>> matrices = matrix_derive(cur.m); for (auto e : matrices) {
if(!in_open(open, e)){
if (!in_close(close, e)) {
open.push_back(Block(cur.g+1, evaluate(e, goal), e));
}
}
}
}
} int main() {
vector<vector<int>> begin = { {2,1,6},{4,0,8},{7,5,3} };
vector<vector<int>> goal = { {1,2,3}, {8,0,4}, {7,6,5} }; search(begin, goal); return 0;
}

八数码问题 Eight Digital Problem的更多相关文章

  1. 八数码问题(8-Puzzle Problem)

    八数码问题(8-Puzzle Problem) P1379 八数码难题 - 洛谷 题目概述:在 \(3 \times 3\) 的棋盘上摆放着 \(8\) 个棋子,棋子的编号分别为 \(1\) 到 \( ...

  2. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  3. poj 1077-Eight(八数码+逆向bfs打表)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  4. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  5. HDU 3567 Eight II(八数码 II)

    HDU 3567 Eight II(八数码 II) /65536 K (Java/Others)   Problem Description - 题目描述 Eight-puzzle, which is ...

  6. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  7. Eight(经典题,八数码)

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. Codeforces 645A Amity Assessment【八数码】

    题目链接: http://codeforces.com/problemset/problem/645/A 题意: 2*2的八数码问题 分析: 这题n为2,不需要搜索,直接判断字母排列顺序就好了. 注意 ...

  9. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

随机推荐

  1. css3径向渐变

    #grad2 { height: 440px; width: 440px; border-radius: %; background: -webkit-radial-gradient(closest- ...

  2. 服务器--apache启用多个端口的方法

    apache启用多个端口的方法 使用本地ip:端口号,或者修改hosts文件+域名的方法来进行本地多站点web调试. 注意这里是用apache 不是iis 1.安装好AppServ2.5.9软件 官网 ...

  3. Linux基础(一)流程控制

    Shell 流程控制 if 语句语法格式1: 写成一行(适用于终端命令提示符):if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo ...

  4. uft调用rfc接口

    RFC接口函数调用: 以下代码是封装好的,为了提供给UFT工具调用,使用c#写成dll. 类型项目分成两个: 1.baseConfigModel.cs  //sap配置登录信息,属性实体类 using ...

  5. mybatis之接口绑定

    接口绑定方案 mybatis中,提供了一套接口绑定方案,程序员可以提供一个接口,然后提供对应接口的一个mapper.xml文件.MyBatis会自动将接口和xml文件进行绑定.实际上就是mybatis ...

  6. shell基础:环境变量

    子shell是在父shell中打开的shell. 使用pstree查看进程树. $调用环境变量 set查看所有变量内容, env查询环境变量 只是临时改变

  7. SpringMVC.入门篇.一.HelloWorld

    SpringMVC.入门篇<一>HelloWorld 项目包结构如下: HelloController.java 代码 package com.charles.controller; im ...

  8. C# HtmlDocument和HtmlNode的使用以及节点的模糊查询

    C#HtmlAgilityPack.HtmlDocument和HtmlAgilityPack.HtmlNode的使用 HtmlAgilityPack.HtmlDocument response = n ...

  9. Steam API调试

    概览 经过这些年,Steam 已经成长为一款大型应用程序,提供多款调试用单独模块及方法.本文将尽量向您呈现这些模块与方法,帮助您充分利用 Steam 与 Steamworks,减少烦恼. Steam ...

  10. .net 缓存

    缓存有很多实现方法,所有这些可以被分为两类,基于内存的缓存和基于磁盘的缓存: 1.  内存驻留缓存——包含在内存中临时存储数据的所有实现方法,通常在以下情况下使用: a)       应用程序频繁使用 ...