您好,欢迎访问三七文档
#includestdio.h#includeconio.hlongintm=0;longintn;typedefstructNode{intmatrix[9];//´æ´¢¾ØÕóintoperate;//¾ØÕóÖС®0¡¯ËùÔÚµÄλÖÃintfather;//Ö¸Ïò²úÉú×ÔÉíµÄ¸¸½áµã}Node;intstart[9];//´Ë´¦Ã»ÓбØÒª³õʼ»¯intend[9];//´Ë´¦Ã»ÓбØÒª³õʼ»¯Nodebase[1000000];intresult[10000];//´æ·Å½á¹ûµÄbaseÊý×éϱêºÅ£¬ÄæÐò´æ·Åintmatch()//ÅжÏÊÇ·ñΪĿ±ê{inti;for(i=0;i9;i++){if(base[n-1].matrix[i]!=end[i])return0;}return1;}voidshow()//ÏÔʾ¾ØÕóµÄÄÚÈÝ{longinti=1;while(m=0){longintmm=result[m];printf(\n\n\n״̬·½¸ñÈçÏ£¬²½Öè%d,i);printf(\n\t\t\t%d\t%d\t%d\n,base[mm].matrix[0],base[mm].matrix[1],base[mm].matrix[2]);printf(\n\n\t\t\t%d\t%d\t%d\n,base[mm].matrix[3],base[mm].matrix[4],base[mm].matrix[5]);printf(\n\n\t\t\t%d\t%d\t%d\n,base[mm].matrix[6],base[mm].matrix[7],base[mm].matrix[8]);m--;i++;}}voidleave()//ÍÆÀí³É¹¦ºóÍ˳ö³ÌÐò֮ǰҪִÐеĺ¯Êý£¬Ö÷Òª×÷ÓÃÊÇÊä³ö½á¹û{n--;while(base[n].father!=-1){result[m]=n;m++;n=base[n].father;}result[m]=0;show();printf(\nËÑË÷³É¹¦£¡½á¹ûÈçÉÏ£¡£¡\n);}intleft(intx)//°ÑϱêΪXµÄÊý×éÖеľØÕóµÄ¿Õ¸ñ×óÒÆ{inti;for(i=0;i9;i++){base[n].matrix[i]=base[x].matrix[i];}base[n].father=x;base[n].matrix[base[x].operate]=base[n].matrix[base[x].operate-1];base[n].matrix[base[x].operate-1]=0;base[n].operate=base[x].operate-1;n++;if(match())leave();return1;}intright(intx)//°ÑϱêΪXµÄÊý×éÖеľØÕóµÄ¿Õ¸ñÓÒÒÆ{inti;for(i=0;i9;i++){base[n].matrix[i]=base[x].matrix[i];}base[n].father=x;base[n].matrix[base[x].operate]=base[n].matrix[base[x].operate+1];base[n].matrix[base[x].operate+1]=0;base[n].operate=base[x].operate+1;n++;if(match())leave();return1;}intup(intx)//°ÑϱêΪXµÄÊý×éÖеľØÕóµÄ¿Õ¸ñÉÏÒÆ{inti;for(i=0;i9;i++){base[n].matrix[i]=base[x].matrix[i];}base[n].father=x;base[n].matrix[base[x].operate]=base[n].matrix[base[x].operate-3];base[n].matrix[base[x].operate-3]=0;base[n].operate=base[x].operate-3;n++;if(match())leave();return1;}intdown(intx)//°ÑϱêΪXµÄÊý×éÖеľØÕóµÄ¿Õ¸ñÏÂÒÆ{inti;for(i=0;i9;i++){base[n].matrix[i]=base[x].matrix[i];}base[n].father=x;base[n].matrix[base[x].operate]=base[n].matrix[base[x].operate+3];base[n].matrix[base[x].operate+3]=0;base[n].operate=base[x].operate+3;n++;if(match())leave();return1;}voidmain(){inti;n=1;//ÒÔÏÂÊÇÊäÈë³õʼºÍÄ¿±ê¾ØÕ󣬲¢°ÑÊäÈëµÄ0ת»»Îª¿Õ¸ñprintf(ÇëÊäÈë9¸ö·ûºÅ×÷Ϊ³õʼ¾ØÕó£¨È磺012345678£©:);for(i=0;i9;i++){scanf(%d,&start[i]);}printf(ÇëÊäÈë9¸ö·ûºÅ×÷ΪĿ±ê¾ØÕó£¨È磺012345678£©:);for(i=0;i9;i++){scanf(%d,&end[i]);}for(i=0;i9;i++){base[0].matrix[i]=start[i];if(start[i]==0)base[0].operate=i;}base[0].father=-1;//ÒÔÉÏÊÇΪµÚÒ»¸öbaseÊý×éÔªËظ³Öµfor(i=0;i1000000;i++){if(n1000000){printf(\nËÑË÷²½Öè´ïµ½ÏµÍ³ÉÏÏÞ£¬ÎÞ·¨ÕÒµ½½á¹û£¡£¡\n);break;}if(base[i].operate==0){right(i);down(i);}if(base[i].operate==1){left(i);right(i);down(i);}if(base[i].operate==2){left(i);down(i);}if(base[i].operate==3){right(i);up(i);down(i);}if(base[i].operate==4){left(i);right(i);up(i);down(i);}if(base[i].operate==5){left(i);up(i);down(i);}if(base[i].operate==6){right(i);up(i);}if(base[i].operate==7){left(i);right(i);up(i);}if(base[i].operate==8){left(i);up(i);}}}
本文标题:八数码C语言代码
链接地址:https://www.777doc.com/doc-4239999 .html