现在的位置: 首页 > 综合 > 正文

拼图游戏分析

2014年01月08日 ⁄ 综合 ⁄ 共 5604字 ⁄ 字号 评论关闭

 

<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
p
{mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:宋体;
mso-bidi-font-family:宋体;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:46.75pt 90.0pt 31.15pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->

背景:拼图游戏!


要将以下的一数组


    1    3    0   
4

    6    2    7   
8

    5    9   10   12

   13   14   11   15

变成以下数组

    1    2   
3    4

    5    6    7   
8

    9   10   11   12

   13   14   15    0

0
代表空

最少需要多少步吗?该怎么走呢?有几种走法?

以下是我编的c
语言:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define SNUM 100
int a[SNUM][SNUM],line,max,NUM;
int c[]={1,0,-1,0},d[]={0,-1,0,1};
int ii[100],jj[100],g,finish=0;
int iii[100],jjj[100];

/**********************
从文件中提取数组****************************/


void geta(int (*p)[SNUM])
{FILE *fp;
int b[100],i,j,k=0;
if((fp=fopen("D://in.txt","r"))==NULL)
{printf("can't not open the file/n");
 exit(0);
}
while(fscanf(fp,"%d",&b[k])!=EOF)
{
    k++;
}
line=sqrt(k);
k=0;
for(i=0;i<line;i++)
for(j=0;j<line;j++)
{
    p[i][j]=b[k];
     k++;
}
fclose(fp);
}

/***************
判断数组是否满足要求****************************/


int ifright(int (*p)[SNUM])
{ int i,j,n=1,m=1;
for(i=0;i<line;i++)
for(j=0;j<line;j++)
{if(p[i][j]==n)
    m++;
    n++;
}
if(n==m+1&&p[line-1][line-1]==0)
 return 1;
else
return 0;
}

/****************
获取0
的行*********************************/


int geti(int (*p)[SNUM])
{int i,j,n;
for(i=0;i<line;i++)
for(j=0;j<line;j++)
if(p[i][j]==0)
 n=i;
return n;
}

/****************
获取0
的列***********************************/


int getj(int (*p)[SNUM])
{int i,j,n;
for(i=0;i<line;i++)
for(j=0;j<line;j++)
if(p[i][j]==0)
n=j;
return n;
}

/*****************
向文件中输入二维数组***********************/


void print(int (*p)[SNUM])
{int i,j;
char a='/n';
 FILE *fp1;
 if((fp1=fopen("D://out.txt","a"))==NULL)
 {printf("can't create the file/n");
  exit(0);
 }
     for(i=0;i<line;i++)
     {     for(j=0;j<line;j++)
         
fprintf(fp1,"%5d",p[i][j]);
          fputc(a,fp1);
          fputc(a,fp1);
     }
         fclose(fp1);
}

/*****************
向文件中输入一维数组*************************/


void printa(int *p)
{int i;
char a='/n';
 FILE *fp2;
 if((fp2=fopen("D://out.txt","a"))==NULL)
 {printf("can't create the file/n");
  exit(0);
 }
     for(i=0;i<max-1;i++)
         
fprintf(fp2,"%5d",p[i]);
          fputc(a,fp2);
         fclose(fp2);
}

/*****************
向文件中输入一个数*************************/


void printc(int n)
{FILE *fp3;
 char a='/n';
 if((fp3=fopen("D://out.txt","a"))==NULL)
 {printf("can't create the file/n");
  exit(0);
 }
 fprintf(fp3,"%d",n);
 fputc(a,fp3);
     fclose(fp3);
}

/*****************
向文件中输入一字符串***********************/


void printx(char *p)
{FILE *fp3;
 char a='/n';
 if((fp3=fopen("D://out.txt","a"))==NULL)
 {printf("can't create the file/n");
  exit(0);
 }
 fputs(p,fp3);
 fputc('/n',fp3);
 fputc('/n',fp3);
 fclose(fp3);
}

/*****************
使文件内容清空**********************************/


void clear()
{FILE *fp3;
 if((fp3=fopen("D://out.txt","w"))==NULL)
 {printf("can't create the file/n");
  exit(0);
 }
 fclose(fp3);
}

/****************
递归函数(主体部分,用了回溯法)******************/


int tray(int (*p)[SNUM],int i,int j,int li,int lj,int num,int max)
{ int q=0,v,u,k,temp,kk;
  if(num<max)
  {  if(ifright(p))
      { print(p);
        printx("

要移动的最小步数:");
        printc(num);
        printx("

坐标:");
        printa(ii);
        printa(jj);
        for(kk=0;kk<num;kk++)
        {iii[kk]=ii[kk];
         jjj[kk]=jj[kk];
        }
        NUM=num;
      q=1;
      finish=1;
      }
  for(k=0;k<4;k++)
  {u=i+c[k];
   v=j+d[k];
   if(u>=0&&u<line&&v>=0&&v<line)
      { if(u==li&&v==lj)
        continue;
        else
       {p[i][j]=p[u][v];
        temp=p[u][v];
        p[u][v]=0;
        ii[num]=u;
        jj[num]=v;
        q=tray(p,u,v,i,j,num+1,max);
       if(q=1)
       {p[u][v]=temp;
       p[i][j]=0;
       }
   }
       }
  }
  }else
      q=1;
  return q;
}

/****************
主函数**********************************/


void main()
{int xi,xj,i,j,k=0;
geta(a);
xi=geti(a);
xj=getj(a);
clear();
printx("the start array is:");
print(a);
printx("the finish array is:");
for(max=0;max<1000;max++)
{ tray(a,xi,xj,xi,xj,0,max);
if(finish==1)
break;
}
printx("the game start!!");
while(k<NUM)
{i=iii[k];
 j=jjj[k];
a[xi][xj]=a[i][j];
xi=iii[k];
xj=jjj[k];
a[i][j]=0;
k++;
printx("next:");
print(a);
}
printx("the game over!!");
}

以上是原代码(总共是198
行)

结果:(内容在out.txt
中)

the start array is:

    1    3    0   
4

    6    2    7   
8

    5    9   10   12

   13   14   11   15

the finish array is:

    1    2    3   
4

    5    6    7   
8

    9   10   11   12

   13   14   15    0


要移动的最小步数:

8

坐标:

    0    1    1   
2    2    2    3   
3
    1    1    0   
0    1    2    2   
3
the game start!!

next:

    1    0    3   
4

    6    2    7   
8

    5    9   10   12

   13   14   11   15

next:

    1    2    3   
4

    6    0    7   
8

    5    9   10   12

   13   14   11   15

next:

    1    2    3   
4

    0    6    7   
8

    5    9   10   12

   13   14   11   15

next:

    1    2    3   
4

    5    6    7   
8

    0    9   10   12

   13   14   11   15

next:

    1    2    3   
4

    5    6    7   
8

    9    0   10   12

   13   14   11   15

next:

    1    2    3   
4

    5    6    7   
8

    9   10    0   12

   13   14   11   15

next:

    1    2    3   
4

    5    6    7   
8

    9   10   11   12

   13   14    0   15

next:

    1    2    3   
4

    5    6    7   
8

    9   10   11   12

   13   14   15    0

the game over!!

 

抱歉!评论已关闭.