PDA

View Full Version : مرتب کردن پازل 3 در 3 به روش حریصانه



payam11
سه شنبه 23 بهمن 1386, 17:38 عصر
سلام دوستان من یه برنامه برای مرتب کردن پازل 3*3 به روش حریصانه نوشتم.که اول اعداد 0 تا 8 رو از کاربر میگیره و بعد به روش حریصانه مرتبش میکنه ولی برای بعضی از حالات جواب نمیده.سورس برنامه رو میزارم اگه کسی کمکم کنه خیلی ممنون میشم.


#include<conio.h>
#include<iostream.h>
struct node
{
int a[3][3],z;
node *l,*r,*t,*d;
}*s,*v;
bool vv;
int b[3][3],c;
class tree
{
node *root;
public:
tree(){root=0;}
node* re(){return root;}
void getnode(node *p);
void pos(node *p)
{
if(p)
{
if(v->a[0][0]==p->a[0][0]&&v->a[0][1]==p->a[0][1]&&v->a[0][2]==p->a[0][2]
&&v->a[1][0]==p->a[1][0]&&v->a[1][1]==p->a[1][1]&&v->a[1][2]==p->a[1][2]
&&v->a[2][0]==p->a[2][0]&&v->a[2][1]==p->a[2][1]&&v->a[2][2]==p->a[2][2])
vv=1;
pos(p->l);
pos(p->d);
pos(p->t);
pos(p->r);
}
}
void he(node *p)
{
int i,j,k=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++,k++)
if(p->a[i][j]!=k)
if(k!=9)
p->z++;
else
if(p->a[2][2]!=0)
p->z++;
}
void pre(node *p)
{
if(p)
{
if(!p->l&&!p->r&&!p->t&&!p->d)
{
if(p->z<c)
{
s=p;
c=s->z;
}
}
pre(p->l);
pre(p->d);
pre(p->t);
pre(p->r);
}
}
};
void tree::getnode(node *p)
{
bool kk=0;
int i,j,k,m,n;
node *q;
if(!root)
{
q=new node;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
q->a[i][j]=b[i][j];
q->z=0;
he(q);
if(q->z==0)
kk=1;
q->l=q->r=q->t=q->d=0;
p=q;
root=q;
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(p->a[i][j]==0)
{
m=i;
n=j;
}
if(kk==0)
{
if(m-1!=-1)
{
node *f;
f=new node;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
f->a[i][j]=p->a[i][j];
k=f->a[m-1][n];
f->a[m-1][n]=0;
f->a[m][n]=k;
v=f;
vv=0;
pos(re());
if(vv)
delete f;
else
{
kk=1;
f->l=f->r=f->t=f->d=0;
f->z=0;
he(f);
p->t=f;
}
}
if(n-1!=-1)
{
node *e;
e=new node;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
e->a[i][j]=p->a[i][j];
k=e->a[m][n-1];
e->a[m][n-1]=0;
e->a[m][n]=k;
v=e;
vv=0;
pos(re());
if(vv)
delete e;
else
{
kk=1;
e->l=e->r=e->t=e->d=0;
e->z=0;
he(e);
p->l=e;
}
}
if(m+1!=3)
{
node *u;
u=new node;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
u->a[i][j]=p->a[i][j];
k=u->a[m+1][n];
u->a[m+1][n]=0;
u->a[m][n]=k;
v=u;
vv=0;
pos(re());
if(vv)
delete u;
else
{
kk=1;
u->l=u->r=u->t=u->d=0;
u->z=0;
he(u);
p->d=u;
}
}
if(n+1!=3)
{
node *o;
o=new node;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
o->a[i][j]=p->a[i][j];
k=o->a[m][n+1];
o->a[m][n+1]=0;
o->a[m][n]=k;
v=o;
vv=0;
pos(re());
if(vv)
delete o;
else
{
kk=1;
o->l=o->r=o->t=o->d=0;
o->z=0;
he(o);
p->r=o;
}
}
if(kk==0)
{
p->z=11;
}
}
}
void main()
{
int i,j;
tree h;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
}
c=10;
while(c!=0)
{
clrscr();
c=10;
h.getnode(s);
h.pre(h.re());
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<s->a[i][j]<<" ";
c=s->z;
}
getch();
}

molla652003
سه شنبه 23 بهمن 1386, 23:57 عصر
با این چک کن :

http://itmashhad.com/acm-icpc-2001-vt1467-55.html#9132

با این هم می تونی مقایسه کنی :

http://forum.codecorona.com/showthread.php?tid=509

sanazi
دوشنبه 14 بهمن 1387, 17:27 عصر
اینم سورس کامل بازی پازل


#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int chap(int vo[],int size)
{
int cu=0;
//araye a ke 3*3 boode va amale chape khaneha ro bar ohde darad
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cu=0;
for(int x=0;x<size;x++)
{
if(a[i][j]==vo[x])
{
cu++;
if(x%2==1)
{
if(j!=2)
cout<<"O ";
else
cout<<"O"<<endl;
}
else
{
if(j!=2)
cout<<"* ";
else
cout<<"*"<<endl;
}
}

}
if(cu==0)
{
if(j!=2)
cout<<a[i][j]<<" ";
else
cout<<a[i][j]<<endl;
}
}
}
cout<<endl;
}
bool sa(int a,int b)
{
if(a==0 || b==0)
return false;
if(a==5 || b==5)
return true;
if(a==1)
{
if(b==2 || b==3 || b==4 || b==7 ||b==9)
return true;
}
if(b==1)
{
if(a==2 || a==3 || a==4 || a==7 || a==9)
return true;
}
if(a==9)
{
if(b==7 || b==8 || b==3 || b==6 || b==1)
return true;
}
if(b==9)
{
if(a==7 || a==8 || a==3 || a==6 ||a==1)
return true;
}
if(a==4)
{
if(b==7)
return true;
if(b==6)
return true;
}
if(b==4)
{
if(a==7)
return true;
if(a==6)
return true;
}
if(a==2)
{
if(b==3 || b==8)
return true;
}
if(b==2)
{
if(a==3 || a==8)
return true;
}
if(a==3)
{
if(b==7 ||b==6)
return true;
}
if(b==3)
{
if(a==7||a==6)
return true;
}
if(a==7)
{
if(b==8)
return true;
}
if(b==7)
{
if(a==8)
return true;
}


return false;
}
int saw(int a,int b)
{
int r,ra;
if(a==5 )
{
if(b>a)
{
r=b-a;
ra=a-r;
}
else
{
r=a-b;
ra=a+r;
}
return ra;
}
if(b==5 )
{
if(a>b)
{
r=a-b;
ra=b-r;
}
else
{
r=b-a;
ra=b+r;
}
return ra;
}
if(a==1)
{
if(b==7 || b==3 || b==9 )
ra=(a+b)/2;
else
{
r=b-a;
ra=b+r;
}
return ra;
}
if(b==1)
{
if(a==7 || a==3 || a==9 )
ra=(a+b)/2;
else
{
r=a-b;
ra=a+r;
}
return ra;
}
if(a==2)
{
if(b==3)
return 1;
if(b==8)
return 5;
}
if(b==2)
{
if(a==3)
return 1;
if(a==8)
return 5;
}
if(a==3)
{
if(b==7 )
return 5;
if(b==9)
return 6;
if(b==6)
return 9;
}
if(b==3)
{
if(a==7 )
return 5;
if(a==9)
return 6;
if(a==6)
return 9;
}
if(a==4)
{
if(b==7)
return 4;
if(b==6)
return 5;
}
if(b==4)
{
if(a==7)
return 4;
if(a==6)
return 5;
}
if(a==6)
{
if(b==9)
return 3;
}
if(b==6)
{
if(a==9)
return 3;
}
if(a==7)
{
if(b==8 )
return 9;
if(b==9)
return 8;
}
if(b==7)
{
if(a==8 )
return 9;
if(a==9)
return 8;
}
if(b==8)
{
if(b==9)
return 7;
}
if(a==8)
{
if(a==9)
return 7;
}
}
int myEn(int a1, int a2 ,int a3,int a4,int a5,int b1,int b2,int b3,int b4)
{
int ra,o=0,o2=0;
if (sa(b1,b2)==true )
{
if(saw(b1,b2)!=a1 && saw(b1,b2)!=a2 && saw(b1,b2)!=a3 && saw(b1,b2)!=b1 && saw(b1,b2)!=b2 )
{
ra=saw(b1,b2);
return ra;
}
}
if(sa(b1,b3)==true)
{
if(saw(b1,b3)!=a1 && saw(b1,b3)!=a2 && saw(b1,b3)!=a3 && saw(b1,b3)!=a4 && saw(b1,b3)!=b1 && saw(b1,b3)!=b2 && saw(b1,b3)!=b3)
{
ra=saw(b1,b3);
return ra;
}
}
if(sa(b2,b3)==true)
{
if(saw(b2,b3)!=a1 && saw(b2,b3)!=a2 && saw(b2,b3)!=a3 && saw(b2,b3)!=a4 && saw(b2,b3)!=b1 && saw(b2,b3)!=b2 && saw(b2,b3)!=b3 )
{
ra=saw(b2,b3);
return ra;
}
}

if(a2==0)
{
srand(time(NULL));
ra=rand()%9+1;
if(ra==a1 && a1!=9)
ra++;
else if(ra==a1 && a1==9)
ra--;

return ra;

}
if(a3==0)
{
ra=saw(a2,a1);
if(ra==a1 || ra==a2 || ra==b1)
{
while(ra==a1 || ra==a2 || ra==b1)
ra=rand()%9+1;
}
return ra;
}
if(a4==0)
{
if( sa(a2,a3)==true)
{
ra=saw(a3,a2);
if(ra!=a1 && ra!=a2 && ra!=b1 && ra!=b2 && ra!=a3)
{
o++;
return ra;
}

}
if(o==0)
{
ra=saw(a3,a1);
if(ra==a1 || ra==a2 || ra==b1 || ra==b2 || ra==a3)
{
while(ra==a1 || ra==a2 || ra==b1 || ra==b2 || ra==a3 )
ra=rand()%9+1;
}
return ra;

}
}
if(a5==0)
{
if( sa(a4,a3)==true)
{
ra=saw(a4,a3);
if(ra!=a1 && ra!=a2 && ra!=b1 && ra!=b2 && ra!=a3 && ra!=b3 && ra!=a4)
{
o++;
return ra;
}

}
if(o==0&& sa(a4,a2)==true)
{
ra=saw(a4,a2);
if(ra==a1 && ra==a2 && ra==b1 && ra==b2 && ra==a3&& ra!=b3 && ra!=a4)
{
o2++;
return ra;
}


}
if(o2==0)
{
ra=saw(a4,a1);
if(ra==a1 || ra==a2 || ra==b1 || ra==b2 || ra==a3 || ra==a4 || ra==b3)
{
while(ra==a1 || ra==a2 || ra==b1 || ra==b2 || ra==a3|| ra==b3 || ra==a4)
ra=rand()%9+1;
}

return ra;
}
}
}
int wonsh(int my[],int size,int your[],int size2)
{
for(int i=1;i<6;i++)
{
if(your[i]==1)
{
for(int j=1;j<6;j++)
{
if(your[j]==2)
{
for(int x=0;x<6;x++)
{
if(your[x]==3)
return 1;
}
}
if(your[j]==4)
{
for(int x=0;x<6;x++)
{
if(your[x]==7)
return 1;
}
}
if(your[j]==5)
{
for(int x=0;x<6;x++)
{
if(your[x]==9)
return 1;
}
}
}
}
else if(your[i]==5)
{
for(int j=1;j<6;j++)
{
if(your[j]==4)
{
for(int x=0;x<6;x++)
{
if(your[x]==6)
return 1;
}
}
if(your[j]==2)
{
for(int x=0;x<6;x++)
{
if(your[x]==8)
return 1;
}
}
if(your[j]==3)
{
for(int x=0;x<6;x++)
{
if(your[x]==7)
return 1;
}
}
}
}
else if(your[i]==3)
{
for(int j=1;j<6;j++)
{
if(your[j]==6)
{
for(int x=0;x<6;x++)
{
if(your[x]==9)
return 1;
}
}
}
}
else if(your[i]==7)
{
for(int j=1;j<6;j++)
{
if(your[j]==8)
{
for(int x=0;x<6;x++)
{
if(your[x]==9)
return 1;
}
}
}
}
}
for(int i=1;i<5;i++)
{
if(my[i]==1)
{
for(int j=1;j<5;j++)
{
if(my[j]==2)
{
for(int x=0;x<5;x++)
{
if(my[x]==3)
return 2;
}
}
if(my[j]==4)
{
for(int x=0;x<5;x++)
{
if(my[x]==7)
return 2;
}
}
if(my[j]==5)
{
for(int x=0;x<5;x++)
{
if(my[x]==9)
return 2;
}
}
}
}
else if(my[i]==5)
{
for(int j=1;j<5;j++)
{
if(my[j]==4)
{
for(int x=0;x<5;x++)
{
if(my[x]==6)
return 2;
}
}
if(my[j]==2)
{
for(int x=0;x<5;x++)
{
if(my[x]==8)
return 2;
}
}
if(my[j]==3)
{
for(int x=0;x<5;x++)
{
if(my[x]==7)
return 2;
}
}
}
}
else if(my[i]==3)
{
for(int j=1;j<5;j++)
{
if(my[j]==6)
{
for(int x=0;x<5;x++)
{
if(my[x]==9)
return 2;
}
}
}
}
else if(my[i]==7)
{
for(int j=1;j<5;j++)
{
if(my[j]==8)
{
for(int x=0;x<5;x++)
{
if(my[x]==9)
return 2;
}
}
}
}
}
if(my[1]!=0 && my[2]!=0 && my[3]!=0 && my[4]!=0 && your[1]!=0 && your[2]!=0 && your[3]!=0 && your[4]!=0 && your[5]!=0)
return 5;
else
return 0;
}
int main()
{
int g=0;
while(g==0)
{
int i=-1,z=1,x=1,co=1;
int vo[9]={0};
int my[5]={0},your[6]={0};
chap( vo,9);
while(wonsh(my,5,your,6)==0)
{
co=1;
i++;
while(co!=0)
{
co=0;
cout<<"inter your choice:";
cin>>vo[i];
if(vo[i]>9 || vo[i]<1)
{
cout<<"in adad na motabar ast"<<endl;
co++;
}
else
{
for(int y=0;y<i;y++)
{
if(vo[y]==vo[i] )
{
cout<<"in khane ghablan entekhab shode.lotfan khane jadid entekhab konid."<<endl;
co++;
}
}
}
}
cout<<endl;
your[z]=vo[i];
i++;
z++;
vo[i]=myEn(your[1],your[2],your[3],your[4],your[5],my[1],my[2],my[3],my[4]);
my[x]=vo[i];
x++;
chap(vo,9);
}
if(wonsh(my,5,your,6)==1)
cout<<"Player won"<<endl;
if(wonsh(my,5,your,6)==2)
cout<<"computer won"<<endl;
if(wonsh(my,5,your,6)==5)
cout<<"mosavi"<<endl;
cout<<endl<<"pres any number to quit"<<endl<<"pres 0 to retry"<<endl;
cin>>g;
cout<<endl;
}
return 0;

}

farzad_vb62
دوشنبه 17 فروردین 1388, 10:38 صبح
به جاي نوشتن کد بهتر نبود آپلود مي کردين؟!!؟
چون تو کپي مشکل پيش مياد...