8 puzzle 问题

lihorse_1043 2009-04-01 08:08:34
输入按照以下格式:
i0 i3 i6
i1 i4 i7
i2 i5 i8

例如:
2 3 5
1 8 6
7 0 4
程序通过移动数字0完成整个过程,然后返回数字零的所有移动过程(l r r u d d l)
运行后结果是:
1 2 3
8 0 4
7 6 5
请教大侠们,我用数组将原始数据读入后,想用队列实现breadth-first算法,可是如何将移动前的步骤,和
移动后的步骤记录下来。待整个算法运行结束后好返回所有步骤。
是否可以将函数压入,队列或堆栈?
以下是我读入数据得程序,进行不下去了,忘哪位略指点1,2,不胜感激

#include<iostream>
using namespace std;
int i[9];
int position;
void input();
void print();
void u(int k);
void d(int k);
void l(int k);
void r(int k);
void breadth_first();
void depth_first();
void a_start();

void input()
{
int k;
cout<<"input 9 numbers (0,1.....8) following this position:"<<endl;
cout<<"i0 i3 i6"<<endl;
cout<<"i1 i4 i7"<<endl;
cout<<"i2 i5 i8"<<endl<<endl;
cout<<"i0....i8:";
cin>>i[0]>>i[1]>>i[2]>>i[3]>>i[4]>>i[5]>>i[6]>>i[7]>>i[8];
cout<<endl;
// cout<<i[0]<<" "<<i[3]<<" "<<i[6]<<endl;
// cout<<i[1]<<" "<<i[4]<<" "<<i[7]<<endl;
// cout<<i[2]<<" "<<i[5]<<" "<<i[8]<<endl<<endl;
for(k=0;k<9;k++)
{
if(i[k]==0)
position=k;
}
}

void print()
{
cout<<i[0]<<" "<<i[3]<<" "<<i[6]<<endl;
cout<<i[1]<<" "<<i[4]<<" "<<i[7]<<endl;
cout<<i[2]<<" "<<i[5]<<" "<<i[8]<<endl<<endl;
}

void u(int k) //是否要在每个函数中建立一个数组来保存移动前的原始数据
{
int temp;
temp=i[k];
i[k]=i[k-1];
i[k-1]=temp;
position=k-1;
}

void d(int k)
{
int temp;
temp=i[k];
i[k]=i[k+1];
i[k+1]=temp;
position=k+1;
}

void l(int k)
{
int temp;
temp=i[k];
i[k]=i[k-3];
i[k-3]=temp;
position=k-3;
}

void r(int k)
{
int temp;
temp=i[k];
i[k]=i[k+3];
i[k+3]=temp;
position=k+3;
}
...全文
196 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengde007 2009-04-01
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20031229/17/2615234.html
chin_chen 2009-04-01
  • 打赏
  • 举报
回复
网上关于这个的,太多了。
lihorse_1043 2009-04-01
  • 打赏
  • 举报
回复
那位大虾帮忙看看下面这段代码,我找不到错,但是不能运行,谢了
#include<iostream>
using namespace std;
int i[9]; //initial state
int temp[9];
int g[9]={1,8,7,2,0,6,3,4,5};
int direction[5];
int position;

void input();
void print();
void direct(int k);
int chk(int k);
void copy();
void copyt();

int min(int a,int b,int c,int d);

void up(int k);
void down(int k);
void left(int k);
void right(int k);
void breadth_first();
void depth_first();
int a_star();

void input()
{
int k;
cout<<"input 9 numbers (0,1.....8) following this position:"<<endl;
cout<<"i0 i3 i6"<<endl;
cout<<"i1 i4 i7"<<endl;
cout<<"i2 i5 i8"<<endl<<endl;
cout<<"i0....i8:";
cin>>i[0]>>i[1]>>i[2]>>i[3]>>i[4]>>i[5]>>i[6]>>i[7]>>i[8];
cout<<endl;
for(k=0;k<9;k++)
{
if(i[k]==0)
position=k;
}
}

void print()
{
cout<<i[0]<<" "<<i[3]<<" "<<i[6]<<endl;
cout<<i[1]<<" "<<i[4]<<" "<<i[7]<<endl;
cout<<i[2]<<" "<<i[5]<<" "<<i[8]<<endl<<endl;
}

void direct(int k)
{
k=position;
if(k==0||k==3||k==6)
direction[0]=0;
else
direction[0]=1;

if(k==2||k==5||k==8)
direction[1]=0;
else
direction[1]=1;

if(k==0||k==1||k==2)
direction[2]=0;
else
direction[2]=1;

if(k==6||k==7||k==8)
direction[3]=0;
else
direction[3]=1;

}

int chk(int k)
{
int counter=0;
for(k=0;k<9;k++)
{
if(temp[k]!=g[k])
counter++;
}
return counter;
}

void copy()
{
int k;
for(k=0;k<9;k++)
temp[k]=i[k];
}

void copyt()
{
int k;
for(k=0;k<9;k++)
i[k]=temp[k];
}

int min(int a, int b, int c, int d)
{
if(a<b)
{
if(a<c)
{
if(a<d)
return 0;
else return 3;
}
else
{
if(c<d)
return 2;
else return 3;
}
}
else
{
if(b<c)
{
if(b<d)
return 1;
else return 3;
}
else
{
if(c<d)
return 2;
else return 3;
}
}
}
void up(int k)
{
int temp;
temp=i[k];
i[k]=i[k-1];
i[k-1]=temp;
}

void down(int k)
{
int temp;
temp=i[k];
i[k]=i[k+1];
i[k+1]=temp;
}

void left(int k)
{
int temp;
temp=i[k];
i[k]=i[k-3];
i[k-3]=temp;
}

void right(int k)
{
int temp;
temp=i[k];
i[k]=i[k+3];
i[k+3]=temp;
}

void breadth_first()
{}

void depth_first()
{}

int a_start()
{
int k,u,d,l,r;
u=99;
d=99;
l=99;
r=99;
k=position;
if(chk(k)==0)
{
cout<<"The puzzle is sorted."<<endl;
return 0;
}
direct(k);
if(direction[0]==1)
{
copy();
up(k);
u=chk(k);
}
if(direction[1]==1)
{
copy();
down(k);
d=chk(k);
}
if(direction[2]==1)
{
copy();
left(k);
l=chk(k);
}
if(direction[3]==1)
{
copy();
right(k);
r=chk(k);
}
switch (min(u,d,l,r))
{
case 0:
copy();
up(k);
cout<<"u ";
position--;
copyt();
a_star();
case 1:
copy();
down(k);
cout<<"d ";
position++;
copyt();
a_star();
case 2:
copy();
left(k);
cout<<"l ";
position=position-3;
copyt();
a_start();
case 3:
copy();
right(k);
cout<<"r ";
position=position+3;
copyt();
a_start();
}
return 0;
}

int main(){

input();
print();
cout<<"The movement is:";
a_star();
return 0;
}

64,642

社区成员

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

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