#include<iostream>
#include<vector>
#include<string>
using namespace std;
class node
{
public:
string state;
node* father;
int zp;//0的位置
node()
{
state="012345678";
father=NULL;
}
void findzero()
{
for(int i=0;i<9;i++)
{
if(state[i]=='0')
zp=i;
}
}
};
bool isempty(vector<node*> open)
{
if(open.empty())
return 1;
return 0;
}
bool findexist(node* temp,vector<node*> open,vector<node*> close)//判断是否在open,close表中出现过
{
for (int i=0;i<open.size();++i)
{
if (temp->state==open[i]->state)
{
return 1;
}
}
for (int j=0;j<close.size();++j)
{
if (temp->state==close[j]->state)
{
return 1;
}
}
return 0;
}
void deletefirst(vector<node*> &open)//删除open表的第一个元素
{
vector<node*>::iterator k =open.begin();
open.erase(k);
k=open.begin();
}
void extend(node* a,vector<node*> &open,vector<node*> &close)
{
a->findzero();
int zp=a->zp;
node* p1=new node;
if(zp>2)//向上移
{
p1->state=a->state;
char t=p1->state[zp-3];
p1->state[zp-3]=p1->state[zp];
p1->state[zp]=t;
if(!findexist(p1,open,close))
{
p1->father=a;
open.push_back(p1);
}
}
node* p2=new node;
if(zp<6)//往下移
{
p2->state=a->state;
char t=p2->state[zp+3];
p2->state[zp+3]=p2->state[zp];
p2->state[zp]=t;
if(!findexist(p2,open,close))
{
p2->father=a;
open.push_back(p2);
}
}
node* p3=new node;
if(zp%3==0||(zp-1)%3==0)//向右移
{
p3->state=a->state;
char t=p3->state[zp+1];
p3->state[zp+1]=p3->state[zp];
p3->state[zp]=t;
if(!findexist(p3,open,close))
{
p3->father=a;
open.push_back(p3);
}
}
node* p4=new node;
if((zp-2)%3==0)//向左移
{
p4->state=a->state;
char t=p4->state[zp-1];
p4->state[zp-1]=p4->state[zp];
p4->state[zp]=t;
if(!findexist(p4,open,close))
{
p4->father=a;
open.push_back(p4);
}
}
close.push_back(a);
}
void stdprint(string a)//格式输出
{
for(int i=0;i<9;i++)
{
cout<<a[i];
if((i+1)%3)
cout<<' ';
else
cout<<endl;
}
}
int main()
{
vector<node*> open,close,globle;
node *head=new node;
head->state="283104765";
//cin>>head->state;
head->father=NULL;
node *goal=new node;
goal->state="238104765";
//cin>>goal->state;
goal->father=NULL;
open.push_back(head);
while(1)
{
if(isempty(open))
{
break;
}
node *temp=new node;
deletefirst(open);
if(temp->state==goal->state)
{
goal=temp;
break;
}
extend(temp,open,close);
}
while (goal->father=NULL)
{
{
globle.push_back(goal);
goal=goal->father;
}
for (int i=globle.size()-1;i>=0;--i)
{
stdprint(globle[i]->state);
}
}
return 0;
}
该回答引用NewBing,仅供参考,不保证完全正确
你好,这是必应。我可以尝试帮你找出代码的问题。😊
根据我搜索的结果,你的代码可能有以下几个问题:
data.is_open()
和 data.peek() != EOF
是否为真,如果不是,就输出错误信息并退出程序。checked
函数来判断是否达到目标状态,如果是,就输出解的路径并退出程序。inorder
函数,这可能导致程序无法显示正确的结果。你应该在找到目标节点后,将其作为参数传递给 inorder
函数,并在函数内部递归地访问其父节点,直到根节点为止。这里有一些有用的资源,可以帮助你更深入地了解 C++ 代码的编写和调试:
希望这些信息能对你有所帮助!如果你还有其他问题,请随时问我。😊
源: 与必应的对话, 2023/6/29
(1) 暴力美学之广度搜索求解重排九宫格问题_c++九宫重排_godgreen的博客-CSDN博客. https://blog.csdn.net/godgreen/article/details/109492330.
(2) A*算法实现九宫重排 - CSDN博客. https://blog.csdn.net/qq_43501512/article/details/111143964.
(3) 九宫重排c++实现_九宫重排c++,c++九宫重排资源-CSDN文库. https://download.csdn.net/download/jenny0329/3853403.
(4) 重排九宫——C++实现BFS DFS搜索 - CSDN博客. https://blog.csdn.net/FourierFisher/article/details/109558356.