HDU2514 Another Eight Puzzle

來源:互聯網
上載者:User
                                           Another Eight Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 716    Accepted Submission(s): 442

Problem DescriptionFill the following 8 circles with digits 1~8,with each number exactly once . Conntcted circles cannot be filled with two consecutive numbers.
There are 17 pairs of connected cicles:
A-B , A-C, A-D
B-C, B-E, B-F
C-D, C-E, C-F, C-G
D-F, D-G
E-F, E-H
F-G, F-H
G-H


Filling G with 1 and D with 2 (or G with 2 and D with 1) is illegal since G and D are connected and 1 and 2 are consecutive .However ,filling A with 8 and B with 1 is legal since 8 and 1 are not consecutive .

In this problems,some circles are already filled,your tast is to fill the remaining circles to obtain a solution (if possivle). 

InputThe first line contains a single integer T(1≤T≤10),the number of test cases. Each test case is a single line containing 8 integers 0~8,the numbers in circle A~H.0 indicates an empty circle.

 

OutputFor each test case ,print the case number and the solution in the same format as the input . if there is no solution ,print “No answer”.If there more than one solution,print “Not unique”. 

Sample Input

37 3 1 4 5 8 0 07 0 0 0 0 0 0 01 0 0 0 0 0 0 0
 

Sample Output

Case 1: 7 3 1 4 5 8 6 2Case 2: Not uniqueCase 3: No answer
 

SourceECJTU 2008 Autumn Contest 

Recommendlcy

首道自過dfs題,必須紀念,折騰了整整一下午才出來的,好有成就感啊有木有。先貼個AC代碼,中間有些注釋是做題時利於自己理解而碼的,親們可以模仿。由於第一次寫,比較拙劣,親們不要見怪啊!

解題思路:本題用DFS解答,是比較經典的DFS題目,其原型可見http://acm.hdu.edu.cn/showproblem.php?pid=1016,該題由原題稍作改編而來,有異曲同工之妙。
解題資料結構:

呵呵!其他的,祥見注釋,祝親們刷題愉快!

先貼個簡潔的代碼:

    #include<cstdio>      #include<cstring>      #include<algorithm>      using namespace std;      int map[9][9];      int x[9],y[9];      int sum;      int temp[9];      void dfs(int t)      {          int i,j;          if(t==9)    //    如果到最後,符合要求,記錄結果          {              sum++;              if(sum==1)                  for(i=0;i<=8;i++)                      temp[i]=x[i];          }          else if(x[t])   //該位置已賦值,跳過               dfs(t+1);        else    //將i賦給x[t]-‘A+t-1’          {              for(i=1;i<=8;i++) //每次都從頭到尾搜尋一遍,判斷有沒有符合要求的              {                  if(!y[i])       //i未使用                  {                      for(j=1;j<=8;j++)   //衝突判斷                          if((map[t][j]||map[j][t])&&x[j]&&abs(i-x[j])==1)                          {                               break;                          }                      if(j==9)    //無衝突                      {                          y[i]=1;    //已使用,標記                          x[t]=i;                          dfs(t+1);                          y[i]=0;    //返回時,撤銷遞迴標誌                          x[t]=0;                      }                  }              }          }      }      int main()      {          int case1=1;          memset(map,0,sizeof(map));          map[1][2]=map[1][3]=map[1][4]=1;          map[2][3]=map[2][5]=map[2][6]=1;          map[3][4]=map[3][5]=map[3][6]=map[3][7]=1;          map[4][6]=map[4][7]=1;          map[5][6]=map[5][8]=1;          map[6][7]=map[6][8]=1;          map[7][8]=1;          int n,i;          scanf("%d",&n);          while(n--)          {              memset(y,0,sizeof(y));              for(i=1;i<=8;i++)              {                  scanf("%d",&x[i]),y[x[i]]=1;              }              printf("Case %d: ",case1++);              sum=0;              dfs(1);               if(sum==1)              {                  printf("%d",temp[1]);                  for(i=2;i<=8;i++)                      printf(" %d",temp[i]);              }              else if(sum==0)                  printf("No answer");              else                  printf("Not unique");              printf("\n");          }          return 0;      }  

這個是原碼,原汁原味,青春的紀念

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int map[9][9];int x[9],y[9];int sum;int temp[9];void dfs(int t){   // printf("\n\n\t\tt=%d-------",t);    int i,j;    if(t==9)    //    如果到最後,符合要求,記錄結果    {        //printf(" 如果到最後,符合要求,記錄結果\n");        sum++;        if(sum==1)            for(i=0;i<=8;i++)                temp[i]=x[i];       // printf("%d",x[1]);  //輸出測試           // for(i=2;i<=8;i++)                //printf(" %d",x[i]);        //printf("\n");    }    else if(x[t])   //該位置已賦值,跳過         dfs(t+1);//printf("////該位置已賦值,跳過\n"),    else    //將i賦給x[t]-‘A+t-1’    {        //printf("//將i賦給x[t]-‘A+t-1’  t=%d\n",i,t);        for(i=1;i<=8;i++) //每次都從頭到尾搜尋一遍,判斷有沒有符合要求的        {            //printf("---------i=%d\n",i);            if(!y[i])       //i未使用            {               // printf(" //i未使用\n");                for(j=1;j<=8;j++)   //衝突判斷                    if((map[t][j]||map[j][t])&&x[j]&&abs(i-x[j])==1)                    {                        // printf("與x[j]=%d衝突\n",x[j]);                         break;                    }                if(j==9)    //無衝突                {                   //printf("//無衝突\n");                    y[i]=1;    //已使用,標記                    x[t]=i;                    dfs(t+1);                    y[i]=0;    //返回時,撤銷遞迴標誌                    x[t]=0;                }            }        }    }}int main(){    int case1=1;    memset(map,0,sizeof(map));    map[1][2]=map[1][3]=map[1][4]=1;    map[2][3]=map[2][5]=map[2][6]=1;    map[3][4]=map[3][5]=map[3][6]=map[3][7]=1;    map[4][6]=map[4][7]=1;    map[5][6]=map[5][8]=1;    map[6][7]=map[6][8]=1;    map[7][8]=1;    int n,i;    scanf("%d",&n);    while(n--)    {        memset(y,0,sizeof(y));        for(i=1;i<=8;i++)        {            scanf("%d",&x[i]),y[x[i]]=1;        }        printf("Case %d: ",case1++);        sum=0;        dfs(1);        //printf("\n-------\n");        if(sum==1)        {            printf("%d",temp[1]);            for(i=2;i<=8;i++)                printf(" %d",temp[i]);        }        else if(sum==0)            printf("No answer");        else            printf("Not unique");        printf("\n");    }    return 0;}

再貼個不AC的,測試案例直接錯了,但我覺得我的方法值得分享,所以貼上來,親們不用研究哦!我已經發現問題了,就是,中間我有用到X[i]判斷,但遞迴回來時,值沒有複原,多個抹除痕迹的地方就可以了啊,詳見AC代碼!

測試代碼:通過2個案例#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int map[9][9];int x[9],y[9];int sum;int temp[9];void dfs(int t){    printf("\n\n\t\tt=%d-------",t);    int i,j;    if(t==9)    //    如果到最後,符合要求,記錄結果    {        printf(" 如果到最後,符合要求,記錄結果\n");        sum++;        if(sum==1)            for(i=0;i<=8;i++)                temp[i]=x[i];        printf("%d",x[1]);  //輸出測試            for(i=2;i<=8;i++)                printf(" %d",x[i]);        printf("\n");    }    else if(x[t])   //該位置已賦值,跳過         printf("////該位置已賦值,跳過\n"),dfs(t+1);    else    //將i賦給x[t]-‘A+t-1’    {        printf("//將i賦給x[t]-‘A+t-1’  t=%d\n",i,t);        for(i=1;i<=8;i++) //每次都從頭到尾搜尋一遍,判斷有沒有符合要求的        {            printf("---------i=%d\n",i);            if(!y[i])       //i未使用            {                printf(" //i未使用\n");                for(j=1;j<=8;j++)   //衝突判斷                    if((map[t][j]||map[j][t])&&x[j]&&abs(i-x[j])==1)                    {                         printf("與x[j]=%d衝突\n",x[j]);                         break;                    }                if(j==9)    //無衝突                {                    printf("//無衝突\n");                    y[i]=1;    //已使用,標記                    x[t]=i;                    dfs(t+1);                    //y[i]=0;    //返回時,撤銷遞迴標誌                }            }        }    }}int main(){    int case1=1;    memset(map,0,sizeof(map));    map[1][2]=map[1][3]=map[1][4]=1;    map[2][3]=map[2][5]=map[2][6]=1;    map[3][4]=map[3][5]=map[3][6]=map[3][7]=1;    map[4][6]=map[4][7]=1;    map[5][6]=map[5][8]=1;    map[6][7]=map[6][8]=1;    map[7][8]=1;    int n,i;    scanf("%d",&n);    while(n--)    {        memset(y,0,sizeof(y));        for(i=1;i<=8;i++)        {            scanf("%d",&x[i]),y[x[i]]=1;        }        printf("Case %d: \n",case1++);        sum=0;        dfs(1);        printf("\n-------\n");        /*if(sum==1)        {            printf("%d",temp[1]);            for(i=2;i<=8;i++)                printf(" %d",temp[i]);        }        else if(sum==0)            printf("No answer");        else            printf("Not unique");        printf("\n");*/        printf("案例結束,sum=%d\n",sum);    }    return 0;}測試結果:37 3 1 4 5 8 0 0Case 1:                t=1-------////該位置已賦值,跳過                t=2-------////該位置已賦值,跳過                t=3-------////該位置已賦值,跳過                t=4-------////該位置已賦值,跳過                t=5-------////該位置已賦值,跳過                t=6-------////該位置已賦值,跳過                t=7-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3---------i=4---------i=5---------i=6 //i未使用//無衝突                t=8-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用//無衝突                t=9------- 如果到最後,符合要求,記錄結果7 3 1 4 5 8 6 2---------i=3---------i=4---------i=5---------i=6---------i=7---------i=8---------i=7---------i=8-------案例結束,sum=17 0 0 0 0 0 0 0Case 2:                t=1-------////該位置已賦值,跳過                t=2-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1 //i未使用//無衝突                t=3-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3 //i未使用//無衝突                t=4-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=3衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5 //i未使用//無衝突                t=5-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5---------i=6 //i未使用//無衝突                t=6-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5---------i=6---------i=7---------i=8 //i未使用//無衝突                t=7-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=3衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5---------i=6---------i=7---------i=8---------i=7---------i=8---------i=6---------i=7---------i=8---------i=4 //i未使用與x[j]=5衝突---------i=5---------i=6---------i=7---------i=8---------i=2 //i未使用與x[j]=3衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5---------i=6---------i=7---------i=8-------案例結束,sum=01 0 0 0 0 0 0 0Case 3:                t=1-------////該位置已賦值,跳過                t=2-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3 //i未使用//無衝突                t=3-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5 //i未使用//無衝突                t=4-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=1衝突---------i=3---------i=4 //i未使用與x[j]=5衝突---------i=5---------i=6 //i未使用與x[j]=5衝突---------i=7 //i未使用//無衝突                t=5-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=3衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5---------i=6 //i未使用與x[j]=5衝突---------i=7---------i=8 //i未使用//無衝突                t=6-------//將i賦給x[t]-‘A+t-1’  t=2009095316---------i=1---------i=2 //i未使用與x[j]=3衝突---------i=3---------i=4 //i未使用與x[j]=3衝突---------i=5---------i=6 //i未使用與x[j]=5衝突---------i=7---------i=8---------i=8---------i=6 //i未使用與x[j]=7衝突---------i=7---------i=8---------i=4 //i未使用與x[j]=5衝突---------i=5---------i=6 //i未使用與x[j]=5衝突---------i=7---------i=8-------案例結束,sum=0Process returned 0 (0x0)   execution time : 135.859 sPress any key to continue.問題:遞迴回溯失敗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.