Copy
#pragma once
#ifndef _STATE_LIST_H
#define _STATE_LIST_H
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <list>
struct State
{
std::string state;
std::string pre_s;
int space_place;
int g;
double h;
State() : state("1234 5678"), space_place(4) {}
State(const std::string& stat, int _g)
{
g = _g;
state = stat;
space_place = state.find(' ');
}
bool operator==(const State& s) const
{
return state == s.state;
}
bool operator<(const State& s) const
{
return g + h < s.g + s.h;
}
};
std::ostream& operator<<(std::ostream &o, const State& s)
{
for (int i = 0; i < 9; i++)
{
if (s.pre_s.empty() || s.state[i] == s.pre_s[i])
o << " " << s.state[i] << " ";
else
o << "[" << s.state[i] << "]";
if (i % 3 == 2)
o << "\n";
}
return o;
}
template <typename H>
class StateList
{
public:
struct Compare
{
bool operator()(const State& a, const State& b) const
{
return a.state < b.state;
}
};
private:
std::multiset<State> open;
std::set<State, Compare> open_uni;
std::set<State, Compare> closed;
State current;
State target;
H h_func;
void generate_sub_state()
{
State next_state(current);
next_state.g += 1;
next_state.pre_s = current.state;
std::string &s = next_state.state;
int delta[] = { -3, 3, -1, 1 };
int old_place = next_state.space_place;
for (int i = 0; i < 4; i++)
{
int new_place = old_place + delta[i];
if (check(old_place, new_place))
{
std::swap(s[old_place], s[new_place]);
next_state.h = h_func(next_state, target);
next_state.space_place = new_place;
add_to_list(next_state);
std::swap(s[old_place], s[new_place]);
}
}
}
void add_to_list(const State& state)
{
auto iter = open_uni.end();
if ((iter = open_uni.find(state)) != open_uni.end() && iter->g > state.g)
{
if (iter->g > state.g)
{
update_state(*iter, state);
open_uni.erase(iter);
open_uni.insert(state);
}
return;
}
if ((iter = closed.find(state)) != closed.end())
{
if (iter->g > state.g)
{
closed.erase(state);
open.insert(state);
open_uni.insert(state);
}
return;
}
open.insert(state);
open_uni.insert(state);
}
bool check(int old_place, int new_place)
{
bool chk = (0 <= new_place && new_place < 9) &&
(abs(new_place % 3 - old_place % 3) != 2);
return chk;
}
void update_state(const State& old, const State& news)
{
auto i = open.find(old);
while (!(i->state == news.state))
++i;
open.erase(i);
open.insert(news);
}
public:
StateList(const State& from, const State& _target)
{
open.insert(from);
open_uni.insert(from);
current = from;
target = _target;
}
bool is_arrive_target()
{
return current == target;
}
bool is_failed()
{
return !(open.size());
}
State next()
{
if (!is_arrive_target() && open.size() != 0)
{
current = *(open.begin());
open.erase(open.begin());
open_uni.erase(current);
closed.insert(current);
generate_sub_state();
}
return current;
}
std::list<State> get_path()
{
std::list<State> path;
path.push_back(current);
State pre(current);
if (!is_arrive_target())
{
return path;
}
while (!pre.pre_s.empty())
{
pre.state = pre.pre_s;
pre = *(closed.find(pre));
path.push_front(pre);
}
return path;
}
int open_list_size()
{
return open.size();
}
int closed_list_size()
{
return closed.size();
}
};
struct HFunc1
{
double operator()(const State& cur, const State& tar)
{
int cnt = 0;
for (int i = 0; i < 9; i++)
{
cnt += (cur.state[i] != tar.state[i]);
}
return cnt;
}
};
struct HFunc2
{
double operator()(const State& cur, const State& tar)
{
int sum = 0;
for (int i = 0; i < 9; i++)
{
int p1 = cur.state.find(tar.state[i]);
int dis = abs(i / 3 - p1 / 3) + abs(i % 3 - p1 % 3);
sum += dis;
}
return sum;
}
};
struct HFunc3
{
double operator()(const State& cur, const State& tar)
{
return 0;
}
};
#endif
#include "StateList.h"
using std::cout;
using std::string;
int main()
{
typedef HFunc2 HFunc;
State begin("7245 3816", 0);
State target("1234 5678", 0);
begin.h = HFunc()(begin, target);
cout << "from:\n" << begin << "\n";
cout << "to:\n" << target << "\n";
StateList<HFunc> slist(begin, target);
while (!slist.is_arrive_target())
{
if (slist.is_failed())
{
cout << "Failed" << "\n";
return 0;
}
slist.next();
}
auto path = slist.get_path();
for (auto &s : path)
{
cout << s;
printf("(g, f)=(%d, %g)\n\n", s.g, s.g + s.h);
}
printf("final open list size: %d\n", slist.open_list_size());
printf("final closed list size: %d\n", slist.closed_list_size());
return 0;
}
效果图如下,代码会打印出搜索路径

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构