十五数码问题又叫移棋盘问题,有很多种解法。今俺用c写了几种解法,望各位指点。
//**********************************************************
//标题: 关于十五数码问题
//文件名:FifteenNumber.cpp
//版权:由王智安所编写
//***********************************************************
#include <stdio.h>
#include <iostream>
using namespace std;
#define RANK 4 //数组的阶数
#define x 1 //初始棋局中空格所在行
#define y 2 //初始棋局中空格所在列
int InitChess[RANK][RANK]={11,9,4,15,1,3,0,12,7,5,8,6,13,2,10,14};
//初始棋局
int EndChess[RANK][RANK]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
//目标棋局
int Count=0; //记录移动次数
void PrintChess(int chess[RANK][RANK]) //打印棋局
{
int i,j;
for(i=0;i<RANK;i++)
{
for(j=0;j<RANK;j++)
printf("%4d",chess[i][j]);
printf("/n");
}
cout<<endl;
}
void LeftMove(int chess[RANK][RANK],int &row,int &column) //空格左移
{
chess[row][column]=chess[row][column-1];
chess[row][column-1]=0; //用0表示空格
column=column-1; //改变空格所在列
printf("/n/n空格左移后的棋局:/n");
PrintChess(chess);
}
void RightMove(int chess[RANK][RANK],int &row,int &column) //空格右移
{
chess[row][column]=chess[row][column+1];
chess[row][column+1]=0; //用0表示空格
column=column+1; //改变空格所在列
printf("空格右移后的棋局:/n");
PrintChess(chess);
}
void UpMove(int chess[RANK][RANK],int &row,int &column) //空格上移
{
chess[row][column]=chess[row-1][column]; //
chess[row-1][column]=0; //用0表示空格
row=row-1; //改变空格所在行
printf("空格上移后的棋局:/n");
PrintChess(chess);
}
void DownMove(int chess[RANK][RANK],int &row,int &column) //空格下移
{
chess[row][column]=chess[row+1][column]; //
chess[row+1][column]=0; //用0表示空格
row=row+1; //改变空格所在行
printf("空格下移后的棋局:/n");
PrintChess(chess);
}
struct ChessStack //栈中元素的数据结构
{
int chess[RANK][RANK]; //表示棋局
int row,column; //棋局中空格的位置
ChessStack *parentChess; //指向父棋局
ChessStack *pChess; //指向棋局栈中的下一棋局
};
struct Stack //棋局栈的数据结构
{
ChessStack *top; //栈顶
ChessStack *botton; //栈底
};
struct ChessQueue //队列中的数据结构
{
int chess[RANK][RANK]; //表示棋局
int row,column; //棋局中空格的位置
ChessQueue *parentChess; //指向父棋局
ChessQueue *pChess; //指向棋局队列中的后一棋局
};
struct Queue //棋局队列的数据结构
{
ChessQueue *front; //队头
ChessQueue *rear; //队尾
};
struct ChessList //链表中元素的数据结构
{
int chess[RANK][RANK]; //表示棋局
int row,column; //棋局中空格的位置
int depth; //在棋局树中的深度数
int priority; //棋局的优先度
ChessList *parentChess; //指向父棋局
ChessList *pChess; //指向棋局链表中的下一棋局
};
struct List //棋局链表的数据结构
{
ChessList *head; //表头
ChessList *tail; //表尾
};
Stack *OpenChessStack=new Stack; //始状态棋局栈,全局变量
Stack *CloseChessStack=new Stack; //末状态棋局栈,全局变量
Queue *OpenChessQueue=new Queue; //始状态棋局队列,全局变量
Queue *CloseChessQueue=new Queue; //末状态棋局队列,全局变量
List *OpenChessList=new List; //始状态棋局队列,全局变量
List *CloseChessList=new List; //末状态棋局队列,全局变量
int EqualChess(int chess1[RANK][RANK],int chess2[RANK][RANK]) //比较两棋局
{
int i,j; //循环变量
for(i=0;i<RANK;i++) //逐行比较
for(j=0;j<RANK;j++) //逐列比较
if(chess1[i][j]!=chess2[i][j])
return 0; //两棋局不同
return 1; //两棋局相同
}
int CompareChess(int chess1[RANK][RANK],int chess2[RANK][RANK]) //比较两棋局,并返回错位数
{
int different=0; //错位数计数量
int i,j; //循环量
for(i=0;i<RANK;i++) //逐行比较
for(j=0;j<RANK;j++) //逐列比较
{
if(chess1[i][j]!=chess2[i][j])
different++; //对应棋子不同
}
return(different); //返回错位数
}
int EqualStack(int chess[RANK][RANK],Stack *pStack) //判断棋局栈中是否有与本局相同的棋局
{
ChessStack *cStack=pStack->top; //从栈顶开始
while(cStack!=NULL) //直到栈底
{
if(EqualChess(chess,cStack->chess)) //比较两棋局
return 1; //有相同棋局
cStack=cStack->pChess; //指针下移
}
return 0; //没有相同棋局
}
int EqualQueue(int chess[RANK][RANK],Queue *pQueue) //判断棋局队列中是否有与本局相同的棋局
{
ChessQueue *cQueue=pQueue->front; //从队头开始
while(cQueue!=NULL) //直到队尾
{
if(EqualChess(chess,cQueue->chess)) //比较两棋局
return 1; //有相同棋局
cQueue=cQueue->pChess; //指针后移
}
return 0; //没有相同棋局
}
int EqualList(int chess[RANK][RANK],List *pList) //判断棋局链表中是否有与本局相同的棋局
{
ChessList *cList=pList->head; //从表头开始比较
while(cList!=NULL) //直到表尾
{
if(EqualChess(chess,cList->chess)) //比较两棋局
return 1; //有相同棋局
cList=cList->pChess; //指针下移
}
return 0; //没有相同棋局
}
void Push(ChessStack *eleChess,Stack *pStack) // 棋局入栈
{
eleChess->pChess=pStack->top;
pStack->top=eleChess; //改变栈顶
if(pStack->botton==NULL)
pStack->botton=eleChess; //改变栈底
}
ChessStack *Pop(Stack *pStack) //棋局出栈
{
ChessStack *eleChess=pStack->top;
pStack->top=eleChess->pChess; //改变栈顶
if(pStack->top==NULL)
pStack->botton=NULL; //改变栈底
return eleChess; //返回出栈的元素
}
void InQueue(ChessQueue *eleChess,Queue *pQueue) // 棋局入队列
{
if(pQueue->rear==NULL)
pQueue->rear=eleChess;
else
pQueue->rear->pChess=eleChess; //从队尾插入
pQueue->rear=eleChess; //改变队尾
if(pQueue->front==NULL)
pQueue->front=eleChess; //改变队头
}
ChessQueue *OutQueue(Queue *pQueue) //棋局出队列
{
ChessQueue *eleChess=pQueue->front;
pQueue->front=eleChess->pChess; //改变队头
eleChess->pChess=NULL;
if(pQueue->front==NULL)
pQueue->rear=NULL; //改变队尾
return eleChess; //返回出队列的元素
}
void Insert(ChessList *eleChess,List *pList) //棋局链表中插入新元素
{
ChessList *persentPChess,*priorPChess; //指向插入位置的当前指针和指向前一位置的指针
priorPChess=NULL;
persentPChess=pList->head;
while(persentPChess!=NULL&&persentPChess->priority<eleChess->priority)
//根据优先度寻找插入位置
{
priorPChess=persentPChess;
persentPChess=persentPChess->pChess; //指针后移
}
if(persentPChess==NULL) //判断当前位置是否在表尾
pList->tail=eleChess;
if(priorPChess==NULL) //判断当前位置是否在表头
pList->head=eleChess;
else
priorPChess->pChess=eleChess;
eleChess->pChess=persentPChess;
}
ChessList *Remove(List *pList) //棋局链表中移出表头元素
{
ChessList *eleChess=pList->head;
pList->head=eleChess->pChess; //改变表头
eleChess->pChess=NULL;
if(pList->head==NULL)
pList->tail=NULL; //改变表尾
return eleChess; //返回表头元素
}
void CreateChess(int chess1[RANK][RANK],int chess2[RANK][RANK]) //用棋局chess2拷贝出棋局chess1
{
int i,j; //循环量
for(i=0;i<RANK;i++)
for(j=0;j<RANK;j++)
chess1[i][j]=chess2[i][j]; //拷贝
}
ChessStack *DepthSearch(int chess[RANK][RANK],int row,int column)
//深度搜索目标棋局
{
OpenChessStack->top=0; //初始化栈
OpenChessStack->botton=0; //初始化栈
CloseChessStack->top=0; //初始化栈
CloseChessStack->botton=0; //初始化栈
ChessStack *pointChess=new ChessStack; //创建新元素
CreateChess(pointChess->chess,chess);
pointChess->row=row;
pointChess->column=column;
pointChess->parentChess=NULL;
pointChess->pChess=NULL;
Push(pointChess,OpenChessStack); //新元素入栈
ChessStack *tempPStack=pointChess; //始状态棋局栈出栈元素
while(!EqualChess(tempPStack->chess,EndChess)) //循环直到找到目标棋局
{
if((OpenChessStack->top)==0) //判断始状态棋局是否为空
{
cout<<"找不到"<<endl;
return(0);
}
//***************************************/
tempPStack=Pop(OpenChessStack); //始状态棋局出栈
if((tempPStack->column)>0) //判断棋局空格是否在最左边
{
ChessStack *mChess=new ChessStack; //创建新元素
CreateChess(mChess->chess,tempPStack->chess);
mChess->parentChess=tempPStack;
mChess->pChess=OpenChessStack->top;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
LeftMove(mChess->chess,mChess->row,mChess->column);
//变为空格左移的棋局
if((!EqualStack(mChess->chess,OpenChessStack))&&(!EqualStack(mChess->chess,CloseChessStack)))
//判断状态栈中是否有相同的棋局
Push(mChess,OpenChessStack); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
if((tempPStack->column)<RANK-1) //判断棋局空格是否在最右边
{
ChessStack *mChess=new ChessStack; //创建新元素
CreateChess(mChess->chess,tempPStack->chess);
mChess->parentChess=tempPStack;
mChess->pChess=OpenChessStack->top;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
RightMove(mChess->chess,mChess->row,mChess->column);
//变为空格右移的棋局
if((!EqualStack(mChess->chess,OpenChessStack))&&(!EqualStack(mChess->chess,CloseChessStack)))
//判断状态栈中是否有相同的棋局
Push(mChess,OpenChessStack);
else
{delete mChess;}
}
if((tempPStack->row)>0) //判断棋局空格是否在最上边
{
ChessStack *mChess=new ChessStack; //创建新元素
CreateChess(mChess->chess,tempPStack->chess);
mChess->parentChess=tempPStack;
mChess->pChess=OpenChessStack->top;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
UpMove(mChess->chess,mChess->row,mChess->column);
//变为空格上移的棋局
if((!EqualStack(mChess->chess,OpenChessStack))&&(!EqualStack(mChess->chess,CloseChessStack)))
//判断状态栈中是否有相同的棋局
Push(mChess,OpenChessStack); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
if((tempPStack->row)<RANK-1) //判断棋局空格是否在最下边
{
ChessStack *mChess=new ChessStack; //创建新元素
CreateChess(mChess->chess,tempPStack->chess);
mChess->parentChess=tempPStack;
mChess->pChess=OpenChessStack->top;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
DownMove(mChess->chess,mChess->row,mChess->column);
//变为空格下移的棋局
if((!EqualStack(mChess->chess,OpenChessStack))&&(!EqualStack(mChess->chess,CloseChessStack)))
//判断状态栈中是否有相同的棋局
Push(mChess,OpenChessStack); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
Push(tempPStack,CloseChessStack); //末状态棋局入栈
Count++; //移动次数增加一
}
printf("找到了目标棋局/n");
return(tempPStack); //返回找到的目标棋局
}
ChessQueue *BreadthSearch(int chess[RANK][RANK],int row,int column)
//广度搜索目标棋局
{
OpenChessQueue->front=0; //初始化队列
OpenChessQueue->rear=0; //初始化队列
CloseChessQueue->front=0; //初始化队列
CloseChessQueue->rear=0; //初始化队列
ChessQueue *pointChess=new ChessQueue; //创建新元素
CreateChess(pointChess->chess,chess);
pointChess->row=row;
pointChess->column=column;
pointChess->parentChess=NULL;
pointChess->pChess=NULL;
InQueue(pointChess,OpenChessQueue); //新元素入队列
ChessQueue *tempPQueue=pointChess; //始状态棋局队列出队列元素
while(!EqualChess(tempPQueue->chess,EndChess)) //循环直到找到目标棋局
{
if((OpenChessQueue->front)==0) //判断始状态棋局是否为空
{
cout<<"找不到"<<endl;
return(0);
}
//***************************************/
tempPQueue=OutQueue(OpenChessQueue); //始状态棋局出队列
if((tempPQueue->column)>0) //判断棋局空格是否在最左边
{
ChessQueue *mChess=new ChessQueue; //创建新元素
CreateChess(mChess->chess,tempPQueue->chess);
mChess->parentChess=tempPQueue;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
LeftMove(mChess->chess,mChess->row,mChess->column);
//变为空格左移的棋局
if((!EqualQueue(mChess->chess,OpenChessQueue))&&(!EqualQueue(mChess->chess,CloseChessQueue)))
//判断状态队列中是否有相同的棋局
InQueue(mChess,OpenChessQueue); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
if((tempPQueue->column)<RANK-1) //判断棋局空格是否在最右边
{
ChessQueue *mChess=new ChessQueue; //创建新元素
CreateChess(mChess->chess,tempPQueue->chess);
mChess->parentChess=tempPQueue;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
RightMove(mChess->chess,mChess->row,mChess->column);
//变为空格右移的棋局
if((!EqualQueue(mChess->chess,OpenChessQueue))&&(!EqualQueue(mChess->chess,CloseChessQueue)))
//判断状态队列中是否有相同的棋局
InQueue(mChess,OpenChessQueue); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
if((tempPQueue->row)>0) //判断棋局空格是否在最上边
{
ChessQueue *mChess=new ChessQueue; //创建新元素
CreateChess(mChess->chess,tempPQueue->chess);
mChess->parentChess=tempPQueue;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
UpMove(mChess->chess,mChess->row,mChess->column);
//变为空格上移的棋局
if((!EqualQueue(mChess->chess,OpenChessQueue))&&(!EqualQueue(mChess->chess,CloseChessQueue)))
//判断状态队列中是否有相同的棋局
InQueue(mChess,OpenChessQueue); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
if((tempPQueue->row)<RANK-1) //判断棋局空格是否在最下边
{
ChessQueue *mChess=new ChessQueue; //创建新元素
CreateChess(mChess->chess,tempPQueue->chess);
mChess->parentChess=tempPQueue;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
DownMove(mChess->chess,mChess->row,mChess->column);
//变为空格下移的棋局
if((!EqualQueue(mChess->chess,OpenChessQueue))&&(!EqualQueue(mChess->chess,CloseChessQueue)))
//判断状态队列中是否有相同的棋局
InQueue(mChess,OpenChessQueue); //没有相同的棋局
else
{delete mChess;} //有相同的棋局
}
InQueue(tempPQueue,CloseChessQueue); //末状态棋局入队列
Count++; //移动次数增加一
}
printf("找到了目标棋局/n");
return(tempPQueue); //返回找到的目标棋局
}
ChessList *IntelligenceSearch(int chess[RANK][RANK],int row,int column)
//启发式搜索目标棋局
{
OpenChessList->head=0; //初始化链表
OpenChessList->tail=0; //初始化链表
CloseChessList->head=0; //初始化链表
CloseChessList->tail=0; //初始化链表
int different; //错位数计数
ChessList *pointChess=new ChessList; //创建新元素
CreateChess(pointChess->chess,chess);
pointChess->row=row;
pointChess->column=column;
pointChess->depth=0; //初始深度为零
different=CompareChess(chess,EndChess); //计算错位数
pointChess->priority=pointChess->depth+different;//设定优先度
pointChess->parentChess=NULL;
pointChess->pChess=NULL;
Insert(pointChess,OpenChessList); //新元素插入链表
ChessList *tempPList=pointChess; //始状态棋局链表表头元素
while(!EqualChess(tempPList->chess,EndChess)) //循环直到找到目标棋局
{
if((OpenChessList->head)==0) //判断始状态棋局链表是否为空
{
cout<<"找不到"<<endl;
return(0);
}
//***************************************/
tempPList=Remove(OpenChessList); //始状态棋局链表移出表头
if((tempPList->column)>0) //判断棋局空格是否在最左边
{
ChessList *mChess=new ChessList; //创建新元素
CreateChess(mChess->chess,tempPList->chess);
mChess->parentChess=tempPList;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
mChess->depth=mChess->parentChess->depth+1;
//深度比父棋局深一层
LeftMove(mChess->chess,mChess->row,mChess->column);
//变为空格左移的棋局
if((!EqualList(mChess->chess,OpenChessList))&&(!EqualList(mChess->chess,CloseChessList)))
//判断状态链表中是否有相同的棋局
{
different=CompareChess(mChess->chess,EndChess);
//计算错位数
mChess->priority=mChess->depth+different;
//设定优先度
Insert(mChess,OpenChessList); //没有相同的棋局
}
else
{delete mChess;} //有相同的棋局
}
if((tempPList->column)<RANK-1) //判断棋局空格是否在最右边
{
ChessList *mChess=new ChessList; //创建新元素
CreateChess(mChess->chess,tempPList->chess);
mChess->parentChess=tempPList;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
mChess->depth=mChess->parentChess->depth+1;
//深度比父棋局深一层
RightMove(mChess->chess,mChess->row,mChess->column);
//变为空格右移的棋局
if((!EqualList(mChess->chess,OpenChessList))&&(!EqualList(mChess->chess,CloseChessList)))
//判断状态链表中是否有相同的棋局
{
different=CompareChess(mChess->chess,EndChess);
//计算错位数
mChess->priority=mChess->depth+different;
//设定优先度
Insert(mChess,OpenChessList); //没有相同的棋局
}
else
{delete mChess;} //有相同的棋局
}
if((tempPList->row)>0) //判断棋局空格是否在最上边
{
ChessList *mChess=new ChessList; //创建新元素
CreateChess(mChess->chess,tempPList->chess);
mChess->parentChess=tempPList;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
mChess->depth=mChess->parentChess->depth+1;
//深度比父棋局深一层
UpMove(mChess->chess,mChess->row,mChess->column);
//变为空格上移的棋局
if((!EqualList(mChess->chess,OpenChessList))&&(!EqualList(mChess->chess,CloseChessList)))
//判断状态链表中是否有相同的棋局
{
different=CompareChess(mChess->chess,EndChess);
//计算错位数
mChess->priority=mChess->depth+different;
//设定优先度
Insert(mChess,OpenChessList); //没有相同的棋局
}
else
{delete mChess;} //有相同的棋局
}
if((tempPList->row)<RANK-1) //判断棋局空格是否在最下边
{
ChessList *mChess=new ChessList; //创建新元素
CreateChess(mChess->chess,tempPList->chess);
mChess->parentChess=tempPList;
mChess->pChess=NULL;
mChess->row=mChess->parentChess->row;
mChess->column=mChess->parentChess->column;
mChess->depth=mChess->parentChess->depth+1;
//深度比父棋局深一层
DownMove(mChess->chess,mChess->row,mChess->column);
//变为空格下移的棋局
if((!EqualList(mChess->chess,OpenChessList))&&(!EqualList(mChess->chess,CloseChessList)))
//判断状态链表中是否有相同的棋局
{
different=CompareChess(mChess->chess,EndChess);
//计算错位数
mChess->priority=mChess->depth+different;
//设定优先度
Insert(mChess,OpenChessList); //没有相同的棋局
}
else
{delete mChess;} //有相同的棋局
}
Insert(tempPList,CloseChessList); //插入末状态棋局链表
Count++; //移动次数增加一
}
printf("找到了目标棋局/n");
return(tempPList); //返回找到的目标棋局
}
void SaveToFile(FILE *fp,int chess[RANK][RANK]) //把查找过程保存到文件
{
cout<<"正在保存......"<<endl;
int i,j;
fprintf(fp,"第%d步/n",Count);
for(i=0;i<RANK;i++)
{
for(j=0;j<RANK;j++)
fprintf(fp,"%4d",chess[i][j]);
fprintf(fp,"/n");
}
fprintf(fp,"/n");
}
void FreeStackSpace() //释放堆栈的空间
{
ChessStack *pointChess,*tempPoint;
cout<<"正在释放堆栈的空间......"<<endl;
cout<<"正在释放始状态棋局栈的空间......"<<endl;
pointChess=OpenChessStack->top;
while(pointChess!=0)
{
tempPoint=pointChess;
pointChess=pointChess->pChess;
delete(tempPoint);
}
delete(OpenChessStack);
cout<<"正在释放末状态棋局栈的空间......"<<endl;
pointChess=CloseChessStack->top;
while(pointChess!=0)
{
tempPoint=pointChess;
pointChess=pointChess->pChess;
delete(tempPoint);
}
delete(CloseChessStack);
}
void FreeQueueSpace() //释放队列的空间
{
ChessQueue *pointChess,*tempPoint;
cout<<"正在释放队列的空间......"<<endl;
cout<<"正在释放始状态棋局队列的空间......"<<endl;
pointChess=OpenChessQueue->front;
while(pointChess!=0)
{
tempPoint=pointChess;
pointChess=pointChess->pChess;
delete(tempPoint);
}
delete(OpenChessQueue);
cout<<"正在释放末状态棋局队列的空间......"<<endl;
pointChess=CloseChessQueue->front;
while(pointChess!=0) //判断是否释放完毕
{
tempPoint=pointChess;
pointChess=pointChess->pChess;
delete(tempPoint);
}
delete(CloseChessQueue);
}
void FreeListSpace() //释放链表的空间
{
ChessList *pointChess,*tempPoint;
cout<<"正在释放链表的空间......"<<endl;
cout<<"正在释放始状态棋局链表的空间......"<<endl;
pointChess=OpenChessList->head;
while(pointChess!=0)
{
tempPoint=pointChess;
pointChess=pointChess->pChess;
delete(tempPoint);
}
delete(OpenChessList);
cout<<"正在释放末状态棋局链表的空间......"<<endl;
pointChess=CloseChessList->head;
while(pointChess!=0)
{
tempPoint=pointChess;
pointChess=pointChess->pChess;
delete(tempPoint);
}
delete(CloseChessList);
}
void main()
{
FILE *fp;
char sign; //标志是深度搜索还是广度搜索
printf("初始棋局为(其中0表示空格):/n");
PrintChess(InitChess); //打印初始棋局
if(InitChess[x][y]!=0) //判断是否x,y宏定义有错
{
cout<<"x,y宏定义有错"<<endl;
exit(0);
}
cout<<"本程序支持三种棋局搜索,按'd'表示深度搜索,按'b'表示广度搜索,按'i'表示启发式搜索"<<endl;
cin>>sign;
if(sign=='d') //
{
ChessStack *SearchChess=DepthSearch(InitChess,x,y);
//深度搜索
if(SearchChess)
{
printf("目标棋局为:/n");
PrintChess(SearchChess->chess); //打印找到的棋局
cout<<"现在把查找过程保存到文件......"<<endl;
if((fp=fopen("Search.txt","w"))==NULL) //打开当前目录文件Search.txt
{
cout<<"文件打不开"<<endl;
exit(0);
}
cout<<"查找过程为:"<<endl;
for(;SearchChess!=0;SearchChess=SearchChess->parentChess)
{
cout<<"第"<<Count<<"步"<<endl;
SaveToFile(fp,SearchChess->chess); //查找过程保存到文件
PrintChess(SearchChess->chess); //打印查找过程
Count--;
}
fclose(fp);
cout<<"查找过程保存结束,你现在可以在前目录文件Search.txt查看过程"<<endl;
}
FreeStackSpace();
}
else if(sign=='b') //
{
ChessQueue *SearchChess=BreadthSearch(InitChess,x,y);
//广度搜索
if(SearchChess)
{
printf("目标棋局为:/n");
PrintChess(SearchChess->chess); //打印找到的棋局
cout<<"现在把查找过程保存到文件......"<<endl;
if((fp=fopen("Search.txt","w"))==NULL) //打开当前目录文件Search.txt
{
cout<<"文件打不开"<<endl;
exit(0);
}
cout<<"查找过程为:"<<endl;
for(;SearchChess!=0;SearchChess=SearchChess->parentChess)
{
cout<<"第"<<Count<<"步"<<endl;
SaveToFile(fp,SearchChess->chess); //查找过程保存到文件
PrintChess(SearchChess->chess); //打印查找过程
Count--;
}
fclose(fp);
cout<<"查找过程保存结束,你现在可以在当前目录文件Search.txt查看过程"<<endl;
}
FreeQueueSpace();
}
else if(sign=='i')
{
ChessList *SearchChess=IntelligenceSearch(InitChess,x,y);
//启发式搜索
if(SearchChess)
{
printf("目标棋局为:/n");
PrintChess(SearchChess->chess); //打印找到的棋局
cout<<"现在把查找过程保存到文件......"<<endl;
if((fp=fopen("Search.txt","w"))==NULL) //打开当前目录文件Search.txt
{
cout<<"文件打不开"<<endl;
exit(0);
}
cout<<"查找过程为:"<<endl;
for(;SearchChess!=0;SearchChess=SearchChess->parentChess)
{
cout<<"第"<<Count<<"步"<<endl;
SaveToFile(fp,SearchChess->chess); //查找过程保存到文件
PrintChess(SearchChess->chess); //打印查找过程
Count--;
}
fclose(fp);
cout<<"查找过程保存结束,你现在可以在前目录文件Search.txt查看过程"<<endl;
}
FreeListSpace();
}
else
cout<<"按错键!"<<endl;
cout<<"运行结束!"<<endl;
}
关于本十五数码问题源代码使用说明:
通过修改代码开头的设置(如下)
#define RANK 4 //数组的阶数
#define x 1 //初始棋局中空格所在行
#define y 2 //初始棋局中空格所在列
int InitChess[RANK][RANK]={11,9,4,15,1,3,0,12,7,5,8,6,13,2,10,14};
//初始棋局
int EndChess[RANK][RANK]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
//目标棋局
你可以把此代码应用到八数码问题,还可以自由变更初始棋局和目标棋局。
注:此源代码要用VC++环境运行,不能用TC环境运行。