1008 Gnome Tetravex

练习使用DPS的题,不知道有无别的做法,思路不复杂。形式是统计并且进行数字配对。

 1 #include <stdio.h>

 2 

 3 int m,n,f,sub[25][4],note[25],ans[25];

 4 

 5 void ini(){

 6     int i,j,top,right,bottom,left;

 7     for(i=0;i<25;i++){

 8         for(j=0;j<4;j++)

 9             sub[i][j]=0;

10         note[i]=0;

11         ans[i]=0;

12     }

13     m=0;

14     f=n*n;

15     for(i=0;i<f;i++){

16         scanf("%d %d %d %d",&top,&right,&bottom,&left);

17            for(j=0;j<m;j++){

18             if(sub[j][0]==top&&sub[j][1]==right&&sub[j][2]==bottom&&sub[j][3]==left){

19                 note[j]++;  

20                 break;

21             }

22         }

23         if(j==m){

24             sub[m][0]=top;

25             sub[m][1]=right;

26             sub[m][2]=bottom;

27             sub[m][3]=left;

28             note[m]=1;

29             m++; 

30         }

31     }

32 } 

33 

34 int DPS(int pos){

35     if(pos==f)

36         return 1;

37     else{

38         int i;

39         for(i=0;i<m;i++){

40             if(note[i]){

41                 if(pos>=n)

42                     if(sub[ans[pos-n]][2]!=sub[i][0])

43                         continue;

44                 if(pos%n!=0)

45                     if(sub[ans[pos-1]][1]!=sub[i][3])

46                         continue;

47                 note[i]--; 

48                 ans[pos]=i;

49                 if(DPS(pos+1))

50                      return 1;

51                 note[i]++;

52             }  

53         } 

54     }

55     return 0;

56 }

57 

58 int main(){

59     int index=1;

60     while(1){

61         scanf("%d",&n);

62         if(!n)

63             break;

64         ini();

65         if(index>1)

66             printf("\n");

67         printf("Game %d: ",index++);

68         if(DPS(0))

69             printf("Possible\n");

70         else

71             printf("Impossible\n");

72     }

73     return 0; 

74 }

 

你可能感兴趣的:(gnome)