重排九宫格文档格式.docx
- 文档编号:19600335
- 上传时间:2023-01-08
- 格式:DOCX
- 页数:14
- 大小:29.09KB
重排九宫格文档格式.docx
《重排九宫格文档格式.docx》由会员分享,可在线阅读,更多相关《重排九宫格文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
intmain(){
list<
element>
open,close;
//thesearetwolistthatholdsthetreenode.
elementebegin(element:
ebegin);
mygraphG(&
//step1
open.pushtop(&
//G.add(&
-->
theconstructionfunctiondiditverywell.
while
(1){
//step2
if(open.getnum()==0){
cout<
<
"
Thisquestionhasnosolution."
endl;
break;
}
/*if(close.getnum()>
362880){
"
outofbounderror"
}*/
//cout<
open.getnum()<
:
close.getnum()<
//step3
element*n;
close.pushend(n=open.pop());
//step4
if(element:
reach(n)){
solutiongot:
;
G.drawtree(n);
Totalsteps:
n->
layer<
//step5
element*M[4];
//extendthenode
M[0]=n->
goup();
M[1]=n->
godown();
M[2]=n->
goleft();
M[3]=n->
goright();
inti;
for(i=0;
i<
4;
i++){
if(M[i]==NULL)continue;
if(!
G.exist(M[i])){
//a
G.add(M[i],n);
//M[i]->
draw();
if(!
open.exist(M[i])&
&
!
close.exist(M[i]))
open.pushbyforder(M[i]);
}else{
//b&
c
G.changeparent(M[i],n);
}
//7orderhasbeeendownininserting.
//8whenwereachedhere,wewillgotothefront.
}
system("
pause"
);
return0;
}
classes.h
structpoint{
intx;
inty;
point(intpx,intpy){
x=px;
y=py;
classelement{
public:
charm[3][3];
staticchardest[3][3];
staticcharebegin[3][3];
intlayer;
staticdoublek;
intf_val;
element(chars[3][3],intl=0){
layer=l+1;
for(inti=0;
3;
for(intj=0;
j<
3;
j++){
m[i][j]=s[i][j];
this->
f();
pointgetpos(){
if(m[i][j]==0)
returnpoint(i,j);
system("
echounable&
intf(){
intg=0;
for(inti=0;
for(intj=0;
if(m[i][j]==dest[i][j]){
g+=0;
}else{
g+=1;
}
f_val=(int)(layer+k*g);
returnf_val;
element*goup(){
pointp=this->
getpos();
if(p.x>
0){
element*pnew=newelement(m,layer);
pnew->
m[p.x][p.y]=pnew->
m[p.x-1][p.y];
m[p.x-1][p.y]=0;
returnpnew;
returnNULL;
element*godown(){
if(p.x<
2){
m[p.x+1][p.y];
m[p.x+1][p.y]=0;
element*goleft(){
if(p.y>
m[p.x][p.y-1];
m[p.x][p.y-1]=0;
element*goright(){
if(p.y<
m[p.x][p.y+1];
m[p.x][p.y+1]=0;
staticboolreach(element*p){
if(p->
m[i][j]!
=dest[i][j])
returnfalse;
returntrue;
voiddraw(){
cout<
'
|'
cout<
(int)m[i][j]<
'
boolequals(constelement*p){
=this->
m[i][j])
template<
classT>
classlist{
structnode{
T*ptr;
node*next;
};
node*base;
//stackbasepointer
node*top;
//stacktoppointer
intm_num;
public:
list(){
m_num=0;
base=top=NULL;
boolpushtop(T*tp){
if(base==NULL){
base=top=(node*)malloc(sizeof(node));
base->
next=NULL;
}else{
node*tmp;
tmp=(node*)malloc(sizeof(node));
tmp->
next=top;
top=tmp;
top->
ptr=tp;
m_num++;
boolpushbyforder(T*tp){
top->
node*tmp,*find=top;
while(find!
=NULL){
if(tmp->
ptr->
f_val<
find->
f_val){
tmp->
next=find->
next;
find->
next=tmp;
if(find==top){
top=tmp;
}
break;
find=find->
if(find==NULL){
base->
tmp->
base=tmp;
boolpushend(T*p){
base=tmp;
base->
ptr=p;
T*pop(){
if(m_num==0){
returnNULL;
T*tmp;
node*tobedel=top;
tmp=top->
ptr;
top=top->
if(top==NULL){
base=NULL;
deletetobedel;
m_num--;
returntmp;
intgetnum(){
returnm_num;
T*operator[](intindex){
node*tmp=top;
while(index>
0)index--,(tmp=tmp->
next);
returntmp->
boolexist(T*p){
node*tmp=top;
while(NULL!
=tmp){
if(tmp->
equals(p))
returntrue;
tmp=tmp->
returnfalse;
classmygraph{
//
structgnode{
element*ptr;
gnode*father;
//list<
gnode>
child;
thisitemisnolongerneeded--,bymydesign
gnoderoot;
gnode**gnodelist;
。
intnnodelist;
mygraph(element*p){
root.ptr=p;
root.father=NULL;
gnodelist=(gnode**)malloc(sizeof(gnode*)*10240);
gnodelist[0]=&
root;
nnodelist=1;
voidadd(element*pchild,element*pparent){
boolfind=false;
inti=0;
nnodelist;
if(pparent==gnodelist[i]->
ptr){
find=true;
break;
if(!
find){
system("
echolistErr:
No_parent_found&
gnodelist[nnodelist]=(gnode*)malloc(sizeof(gnode));
gnodelist[nnodelist]->
father=gnodelist[i];
ptr=pchild;
nnodelist++;
boolexist(element*p){
i++){
if(gnodelist[i]->
ptr==p){
//boolhasparent(element*pchild){
//nowthatitbelogstothetree,youcancheckwhetherit'
stheroot.
//returngnodelist[0]->
ptr!
=pchild;
//}
voidchangeparent(element*pchild,element*newparent){
//findoutwhetherweshouldchangeit'
sparentpointer
//anditwilldecidewhetherthechild'
spointershouldbechange.
//maybeit'
smeaningistochangethef_valofitschilds
for(i=0;
ptr==pchild){
//wewillsurelyfindit,becauseicopiedthecodeinfunction'
exist'
hha.
if(gnodelist[i]->
father->
f_val>
newparent->
//madeitthechildofasmallersection.
gnodelist[i]->
ptr=newparent;
//}
//elseIhavenothingtodo...
voiddrawtree(element*p){
find)return;
gnode*tmp=gnodelist[i];
while(tmp!
================"
father;
六.心得体会:
通过此次实验,加深了我对广度优先搜索的认识。
但广度优先搜索总可以得到解,而且得到的是路径最短的解。
在具体的编程实现中,如何定义节点的数据类型是个比较难的地方,编写比较长的程序对我来说还是一个比较艰巨的任务,以后还得多加强这方面的能力才行。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 重排 九宫