64,642
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
class Node;
ofstream fout("result.txt");
//起始地图
int startMap[3][3]={4,3,5,2,1,0,7,8,6};
//结束地图
int endMap[3][3]={1,2,3,4,5,6,7,8,0};
bool equals(int map1[3][3] ,int map2[3][3])
{
for(int i = 0;i< 3;i++)
for(int j = 0;j < 3;j++)
if(map1[i][j]!=map2[i][j])
return false;
return true;
}
void printMap(int map[3][3])
{
for(int i = 0;i < 3;i++)
{
for(int j = 0;j < 3;j++)
fout<<map[i][j]<<" ";
fout<<endl;
}
fout<<endl;
}
void printMap_ToScreen(int map[3][3])
{
for(int i = 0;i < 3;i++)
{
for(int j = 0;j < 3;j++)
cout<<map[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
vector<Node> visitedList;
//节点类定义
class Node
{
public:
int depth;
Node* father;
int map[3][3];
int x,y;
Node* sons;
int sonsCount;
Node(Node* curFather = NULL,int curX = 1,int curY = 2);
~Node();
void getSons();
};
void printPath(Node* node)
{
if(node->father == NULL)
{
printMap(node->map);
return;
}
else
{
printPath(node->father);
}
printMap(node->map);
}
Node::Node(Node* curFather,int curX,int curY)
{
this->father = curFather;
this->sons=NULL;
this->sonsCount=0;
this->x = curX;
this->y = curY;
if(this->father==NULL)
this->depth = 1;
else
this->depth = father->depth + 1;
if(NULL == father)
{
for(int i = 0;i < 3;i++)
for(int j = 0; j < 3;j++)
this->map[i][j] = startMap[i][j];
}
else
{
//Copy father->map
for(int i = 0;i < 3;i++)
for(int j = 0; j < 3;j++)
this->map[i][j] = father->map[i][j];
//modify father to sons
this->map[father->x][father->y] = father->map[this->x][this->y];
this->map[this->x][this->y] = 0;
}
}
Node::~Node()
{
if(NULL!=this->sons)
delete []sons;
}
void Node::getSons()
{
vector<Node> tempList;
//找到所有可能的节点
if(x>0)tempList.push_back(Node(this,x-1,y));
if(x<2)tempList.push_back(Node(this,x+1,y));
if(y>0)tempList.push_back(Node(this,x,y-1));
if(y<2)tempList.push_back(Node(this,x,y+1));
//去掉那些已经访问过的Sons //
vector<Node>::iterator iter1,iter2;
for(iter1 = visitedList.begin();iter1 != visitedList.end();iter1++)
for(iter2 = tempList.begin();iter2 != tempList.end();)
if(equals(iter1->map,iter2->map))
iter2=tempList.erase(iter2);
else
iter2++;
this->sonsCount=tempList.size();
if(this->sonsCount>0)
{
this->sons=new Node [sonsCount];
for(int i = 0;i<sonsCount;i++)
sons[i]=tempList[i];
}
}
void breadthFirstSearch(vector<Node> curNodeList) //递归调用,广度优先查找
{
//定义存放下一层节点的向量
vector<Node> nextNodeList;
for(int i=0;i<curNodeList.size();i++)
{
if(equals(curNodeList[i].map,endMap))
{
//打印路径,结束程序
printPath(&curNodeList[i]);
return ;
}
else
{
//curNodeList[i]已经访问过了;
visitedList.push_back(curNodeList[i]);
curNodeList[i].getSons();
//取这个节点的所有子节点 将curNodeList[i]的所有的子节点加入到nextNodeList中;
for( int j=0;j<curNodeList[i].sonsCount;j++)
nextNodeList.push_back(curNodeList[i].sons[j]);
}
}
//向下一层节点广度优先查找
breadthFirstSearch(nextNodeList);
}
void breadthFirstSearch_NotRecursion(vector<Node> curNodeList) //非递归调用,广度优先查找,出错~~~~~~~~~
{
for(int i=0;;i++)
{
if(equals(curNodeList[i].map,endMap))
{
//打印路径,结束程序
printPath(&curNodeList[i]);
return ;
}
else
{
//curNodeList[i]已经访问过了;
visitedList.push_back(curNodeList[i]);
curNodeList[i].getSons();
//取这个节点的所有子节点 将curNodeList[i]的所有的子节点加入到curNodeList中;
for( int j=0;j<curNodeList[i].sonsCount;j++)
curNodeList.push_back(curNodeList[i].sons[j]);
}
}
}
int main()
{
Node root;
vector<Node> rootNodeList;
rootNodeList.push_back(root);
breadthFirstSearch_NotRecursion(rootNodeList);
return 0;
}