八数码问题 Eight Digital Problem
八数码问题
利用启发式搜索,找出以下问题的最优解。

#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的更多相关文章
- 八数码问题(8-Puzzle Problem)
八数码问题(8-Puzzle Problem) P1379 八数码难题 - 洛谷 题目概述:在 \(3 \times 3\) 的棋盘上摆放着 \(8\) 个棋子,棋子的编号分别为 \(1\) 到 \( ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- 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 ...
- HDU 1043 Eight (BFS·八数码·康托展开)
题意 输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数 即这个排列在全部排列中的字典序 然后就是基础的BFS了 #include <bits/stdc++.h ...
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
- Eight(经典题,八数码)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Codeforces 645A Amity Assessment【八数码】
题目链接: http://codeforces.com/problemset/problem/645/A 题意: 2*2的八数码问题 分析: 这题n为2,不需要搜索,直接判断字母排列顺序就好了. 注意 ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
随机推荐
- iOS 添加启动图片
之前添加启动图片,一直都是通过添加LaunchImage来实现,见链接 http://www.cnblogs.com/jys509/p/4856068.html 这种方法,就需要给每个尺寸添加图片. ...
- is_readable() 函数检查指定的文件是否可读。
定义和用法 is_readable() 函数判断指定文件名是否可读. 语法 is_readable(file) 参数 描述 file 必需.规定要检查的文件. 说明 如果由 file 指定的文件或目录 ...
- C++调用openssl实现DES加密解密cbc模式 zeropadding填充方式 pkcs5padding填充方式 pkcs7padding填充方式
============================================== des cbc 加密 zeropadding填充方式 ======================= ...
- Bootstrap-全局样式的文本颜色和背景颜色
.text-五种颜色 文本颜色.text-info文本浅蓝颜色-提示.text-warning文本黄色-警告颜色.text-success文本绿色-成功颜色.text-primary文本深蓝色-警 ...
- Mysql8.0.11安装以及注意事项
一.环境配置 首先在官网下载最新的mysql8.0.11数据库,解压到你需要放置的盘符最好不要有中文,然后新建MYSQL_HOME,参数为mysql解压后安装文件的bin文件路径如我的:变量名:MYS ...
- Message对象
一)描述 1: 每一个Message对象都包含两个对象: (1)google::protobuf::Descriptor 描述对象,是Message所有Filed的一个集合,它又包含了FieldDes ...
- 136. Single Number(位运算)
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- Vue系列之 => 通过vue-resource发起ajax请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux 压缩解压缩命令详解
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- Java多线程-----volatile关键字详解
volatile原理 Java语言提供了一种稍弱的同步机制,即volatile变量,用来确保将变量的更新操作通知到其他线程.当把变量声明为volatile类型后, 编译器与运行时都会注意 ...