The programming design---The Triangle Game

 

<!-- /* 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;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

In the triangle game you start off with six triangles numbered on each edge,as in the example above.You can slideand rotate the triangles so they form a hexagon,but the hexagon is only legal if edges common to two triangles have the same number on them.You may not flip any triangle over.The score for a legal hexagon is to find the highest score that can be achieved with

any six particular triangles.

Input Specification

The input file will contain one or more data sets.Each data set is a sequence of six lines with three integers from 1 to 100 separated by blanks on each line.Each line contains the numbers on the triangles in clockwise orde.Data sets are sepatated by a line containing only an asterisk.The last data set is followed by a line containing only a dollar sign.

Output Specification

For each input data set,the output is a line containing only the word "none" if there are no legal hexagons or the highest score if there is a legal hexagon.

My program:

#include<stdio.h>

#include<iostream.h>

#include<string.h>

int score_highest=0; //the highest score of each set

int first=1;     //first time output;

 

//decide the all triangles are use,if all use return false,else return true

bool usable(int use[])

{

       int sum=0;

       for(int i=0;i<6;i++)

       {

              sum+=use[i];

       }

       if(sum==6)

              return false;

       else

              return true;

}

 

//loop depend integer n;

int circle(int i,int n)

{

       if(i+n>2)

              i+=n-3;

       else

              i+=n;

    return i;

}

 

//clear file

void clearfile()

{

       FILE *pt;

       pt=fopen("output.txt","w");

       fclose(pt);

}

/*

void print(int six[],int use[])

{

FILE *pt;

pt=fopen("output.txt","a");

int i;

for(i=0;i<6;i++)

fprintf(pt,"%d ",use[i]);

fprintf(pt,"/n");

for(i=0;i<18;i++)

fprintf(pt,"%d ",six[i]);

fprintf(pt,"/n");

fclose(pt);

}*/

 

//get the score

int score(int six[])

{

       int i;

       int sum=0;

       for(i=1;i<17;i++)

       {

        sum+=six[i];

              i+=2;

       }

       return sum;

}

 

//output

void mywrite(int n,int first)

{

       FILE *pt;

       pt=fopen("output.txt","a");

       if(!first)

              fprintf(pt,"/n");

       if(n==0)

              fprintf(pt,"None");

       else

              fprintf(pt,"%d",n);

       fclose(pt);

}

 

//sort the triangle

void hexagon(int triangle[][3],int use[],int next,int six[],int index)

{

   

       int i,j,temp;

       if(!usable(use) && six[0]==six[17])

       {

              if(score_highest<score(six))

              {

                     score_highest=score(six);

              }

       }

       else

       {

              for(i=0;i<6;i++)

              {

                     if(use[i]==0)

                     {

                            for(j=0;j<3;j++)

                            {

                                   if(triangle[i][j]==next)

                                   {

                                          six[index++]=triangle[i][j];

                                          six[index++]=triangle[i][circle(j,1)];

                                          six[index++]=triangle[i][circle(j,2)];

                                          use[i]=1;

                                          temp=next;

                                          next=triangle[i][circle(j,2)];

                                          hexagon(triangle,use,next,six,index);

                        next=temp;

                                          index-=3;

                                          use[i]=0;

                                   }

                            }

                           

                     }

              }

             

       }

      

}

 

 

void main()

{

       clearfile();

       FILE *pt;

       int triangle[6][3];//define the six triangles

       int use[6];        //define the status of six triangles

       int six[18];       //define the temp of hexagon

       char temp[5];

       int setNum;        //define the num of sets

       int index;     

       int i,j,k;

       if(NULL==(pt=fopen("input.txt","r")))

       {

              cout<<"can't open input.txt"<<endl;

       }

       else

       {

              setNum=1;

              while(fscanf(pt,"%s",temp)!=EOF)

              {

                     if(temp[0]=='*')

                            setNum++;

              }

              rewind(pt);

              for(k=0;k<setNum;k++)

              {

                     memset(use,0,sizeof(use));

                     memset(six,0,sizeof(six));

                     score_highest=0;

                     for(i=0;i<6;i++)

                     {

                            for(j=0;j<3;j++)

                            {

                                   fscanf(pt,"%d",&triangle[i][j]);

                            }

                     }

                    

                     fscanf(pt,"%s",temp);

                     use[0]=1;

                     index=0;

                     for(i=0;i<3;i++)

                     {

                            six[index++]=triangle[0][i];

                            six[index++]=triangle[0][circle(i,1)];

                            six[index++]=triangle[0][circle(i,2)];

                hexagon(triangle,use,six[index-1],six,index);

                            index-=3;      

                     }

                     mywrite(score_highest,first);

                     first=0;

              }

             

              fclose(pt);

       }

}

Input:

1 4 20

3 1 5

50 2 3

5 2 7

7 5 20

4 7 50

*

10 1 20

20 2 30

30 3 40

40 4 50

50 5 60

60 6 10

*

10 1 20

20 2 30

30 3 40

40 4 50

50 5 60

10 6 60

$

Output:

152

21

None

你可能感兴趣的:(The programming design---The Triangle Game)