if(!
a.ex()[i].hasIt(open)&&!
a.ex()[i].hasIt(closed)){
open.insertAtBack(a.ex()[i]);
}
}
listSort(open);
}
e.listAll(e);
System.out.println("open表为:
");
open.print();
System.out.println("closed表为:
");
closed.print();
}
}
List代码是:
classListNode{
publicEightdata;
publicListNodenext;
publicListNode()
{
next=null;
}
publicListNode(Eighto)
{
data=o;
next=null;
}
publicListNode(Eighto,ListNodenextNode)
{
data=o;
next=nextNode;
}
//ReturntheEightinthisnode
publicEightgetEight()
{
returndata;
}
publicListNodegetnext()
{
returnnext;
}
}
publicclassList{
publicListNodefirstNode;
publicListNodelastNode;
publicintlength;
privateStringname;//Stringlike"list"usedinprinting
publicList(Strings)
{
name=s;
firstNode=lastNode=null;
length=0;
}
//Constructor:
ConstructoranemptyListwith"List"asthename
publicList(){
this("list");
}
publicEightgetFirstNode(){
returnfirstNode.data;
}
//InsertanEightatthefrontoftheListIfListisempty,firstNode
//andlastNoderefertosameEight.Otherwise,firstNodereferstonewnode.
publicvoidinsertAtFront(EightinsertItem)
{
if(isEmpty())//如果链表为空,返回true,否则返回false.
firstNode=lastNode=newListNode(insertItem);
else
firstNode=newListNode(insertItem,firstNode);
length++;
}
//InsertanEightattheendoftheListIfListisempty,firstNodeand
//lastNoderefertosameEight.Otherwise,lastNode'snextinstancevariablereferstonewnode.
publicvoidinsertAtBack(EightinsertItem)
{
if(isEmpty())
firstNode=lastNode=newListNode(insertItem);
else
lastNode=lastNode.next=newListNode(insertItem);
length++;
}
//RemovethefirstnodefromtheList.
publicEightremoveFromFront()throwsEmptyListException
{
EightremoveItem=null;
if(isEmpty())
thrownewEmptyListException(name);
removeItem=firstNode.data;//retrievethedataresetthefirstNodeandlastNodereferences
if(firstNode.equals(lastNode))
firstNode=lastNode=null;
else
firstNode=firstNode.next;
length--;
returnremoveItem;
}
//RemovethelastnodefromtheList
publicEightremoveFromBack()throwsEmptyListException
{
EightremoveItem=null;
if(isEmpty())
thrownewEmptyListException(name);
removeItem=lastNode.data;//retrievethedataresetthefirstNodeandlastNodereferences
if(firstNode.equals(lastNode))
firstNode=lastNode=null;
else
{
ListNodecurrent=firstNode;
while(current.next!
=lastNode){
current=current.next;
}
lastNode=current;
current.next=null;
}
length--;
returnremoveItem;
}
//ReturntrueiftheListisempty.
publicbooleanisEmpty(){
returnfirstNode==null;
}
//OutputtheListcontents
publicvoidprint()
{
if(isEmpty()){
System.out.println("Empty");
return;
}
ListNodecurrent=firstNode;
do
{
current.data.print();
System.out.println("fֵΪ:
"+current.data.f());
System.out.println("=====");
current=current.next;
}while(current!
=null);
}
}
//ClassEmptyListExceptiondefinition
classEmptyListExceptionextendsRuntimeException{
publicEmptyListException(Stringname)
{
super("The"+name+"isempty");
}
}
程序截图如下:
深度优先算法
#include
#include"iostream.h"
#include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"string.h"
#include
#include
usingnamespacestd;
constintN=3;//3*3图
enumDirection{None,Up,Down,Left,Right};//方向
staticintn=0;
structMap//图
{
intcell[N][N];//数码数组
DirectionBelockDirec;//所屏蔽方向
intstep;
structMap*Parent;//父节点
};
//打印图
voidPrintMap(structMap*map)
{
cout<<"***********************"<for(inti=0;i{
for(intj=0;j{
cout<cell[i][j]<<"";
}
cout<}
cout<<"***********************"<}
//移动图
structMap*MoveMap(structMap*map,DirectionDirect,boolCreateNewMap)
{
structMap*NewMap;
//获取空闲格位置
inti,j;
for(i=0;i{
boolHasGetBlankCell=false;
for(j=0;j{
if(map->cell[i][j]==0)
{
HasGetBlankCell=true;
break;
}
}
if(HasGetBlankCell)
break;
}
//移动数字
intt_i=i,t_j=j;
boolAbleMove=true;
switch(Direct)//判断沿direct所指方向移动数字是否被允许
{
caseDown:
t_i++;
if(t_i>=N)
AbleMove=false;
break;
caseUp:
t_i--;
if(t_i<0)
AbleMove=false;
break;
caseLeft:
t_j--;
if(t_j<0)
AbleMove=false;
break;
caseRight:
t_j++;
if(t_j>=N)
AbleMove=false;
break;
};
if(!
AbleMove)//不可以移动则返回原节点
{
returnmap;
}
if(CreateNewMap)
{
NewMap=newMap();
for(intx=0;xfor(inty=0;yNewMap->cell[x][y]=map->cell[x][y];
}
elseNewMap=map;
NewMap->cell[i][j]=NewMap->cell[t_i][t_j];
NewMap->cell[t_i][t_j]=0;
returnNewMap;
}
//初始化一个初始图
//由目标图生成初始图,保证可以获得结果
structMap*RandomMap(conststructMap*map)
{
intM=30;//随机移动图步数
structMap*NewMap;
NewMap=newMap();
memcpy(NewMap,map,sizeof(Map));
srand((unsigned)time(NULL));
for(inti=0;i{
intDirect=rand()%4;
NewMap=MoveMap(NewMap,(Direction)Direct,false);
}
returnNewMap;
}
//初始图的另种生成方式,随机生成各位置的数
//此方式生成的图在有限次搜索中若深度超过5则多数无解
structMap*RandomMap()
{
inta[9];
structMap*NewMap;
NewMap=newMap();
srand((unsigned)time(NULL));
for(intk=0;k<9;k++)
{
boolIsre=false;
a[k]=rand()%9;
for(intl=0;l{
if(a[k]==a[l])
{
Isre=true;
break;
}
}
if(Isre)
{
k=k-1;
continue;
}
}
for(inti=0;i{
for(intj=0;j{
NewMap->cell[i][j]=a[i*3+j];
}
}
NewMap->Parent=NULL;
NewMap->BelockDirec=None;
returnNewMap;
}
//判断是否搜索成功
boolIsSuccess(structMap*map,structMap*Target)
{
boolIsSuc=true;
for(inti=0;i{
for(intj=0;j{
if(map->cell[i][j]!
=Target->cell[i][j])
{
IsSuc=false;
break;
}
}
if(!
IsSuc)
break;
}
returnIsSuc;
}
structMap*DNF_Search(structMap*begin,structMap*Target,intdm)
{
structMap*p1,*p2,*T=NULL;
stack
stack
OPEN.push(begin);
do
{
p1=OPEN.top();
OPEN.pop();
if(IsSuccess(p1,Target))