八数码问题 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的八数码表,要求程序复原 ...
随机推荐
- 那种多空计算方法更正确呢?——从此图看应该是TEST005
那种方法计算多空逆转更正确呢?——从此图1看应该是TEST005,但是实际上是ZCL_多空! TEST005具有滞后性!也就是说跌了一些在报警,可能已经跌了10%(如图2) ZCL_多空:当计算结果和 ...
- 关于调用数据库函数executeUpdate抛出异常
2018.06.11写一个web程序的时候发现了一个问题,解决了好几天都没解决,并且也找不到问题所在.偶然百度找到了根源所在,希望可以帮到大家. 1 在调用这个函数的时候一直抛异常.刚开始我还以为是代 ...
- .NET 黑魔法 - 自定义日志扩展
我们开发程序时避免不了要有日志系统,我们希望有一个通用的.不夹杂任何方言的调用方式,简单地说就是保留微软日志框架的注入方式和使用方式. 比如我们希望这样调用: 我们不希望有个 IAbcLogger, ...
- 并查集(disjoint)
合并,查找的集合 第一,建立两个函数,一个用来查找(find),一个用来合并(merge): 如下的一个模板: 题目: 亲戚(Relations) 或许你并不知道,你的某个朋友是你的亲戚.他可能是你的 ...
- db2修改表结构,增加列,删除列等
增加一列:ALTER TABLE tbname ADD COLUMN columnName datatype; 增加多列:ALTER TABLE tbname ADD COLUMN columnNam ...
- java中垃圾回收机制中的引用计数法和可达性分析法(最详细)
首先,我这是抄写过来的,写得真的很好很好,是我看过关于GC方面讲解最清楚明白的一篇.原文地址是:https://www.zhihu.com/question/21539353
- JSP—简介
BS/CS的区别? CS模式: client:客户端:存放操作界面的图片样式本地数据和缓存等 server:服务端:保存核心数据 请求响应模式:收到请求后,服务器只需要返回核心的数据 优缺点:需要安装 ...
- Python二分法查找
1.1二分前提是有序,,否则不可以2分,2分查找的时间复杂度是O(log n):排序后二分查找到适当的位置插入数值 lst = [37,99,73,48,47,40,40,25,99,51] def ...
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
- HDU 1014 Uniform Generator(题解)
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...