请路过的大神帮忙看下代码!!

Dr_FIA 2011-03-19 11:34:05
8数码问题(数字拼图)代码
递归方法完美运行,非递归方法出错,不解!!


#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;
}
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
FZNPLY 2011-03-22
  • 打赏
  • 举报
回复
帖子被删了。。。很不满。。。
美女,程序出错原因应该是:
Vector<Node> curNodeList 本来只有一个数据项,当你插入一个数据时,curNodeList变成了两个数据项,这时候Vector<Node> curNodeList进行了自身调整,它先Vector中原有的数据复制到一个更大的空间中,然后删除原有的存储空间,这个时候会调用Node的析构函数,执行了delete []sons;而第三个数据项添加又通过sons指针引用了原来的内存,因此第三个数据项,也就是第二个子节点,添加时会出错。
Dr_FIA 2011-03-19
  • 打赏
  • 举报
回复
运行到把根节点的第二个子节点中即出错,希望能帮我调一下
Dr_FIA 2011-03-19
  • 打赏
  • 举报
回复
非递归和递归相比,只改了3、4行代码,逻辑上感觉没问题,求高手解答!

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧