Uva-129 Krypton Factor

 1 #include <bits/stdc++.h>
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 using namespace std;
 4 
 5 
 6 const char a[27] = {'A','B','C','D','E','F','G','H','I','J',
 7                     'K','L','M','N','O','P','Q','R','S','T',
 8                     'U','V','W','X','Y','Z'
 9                    };
10 
11 int n,L;
12 int cnt;
13 int s[82];
14 
15 int judge(int cur)
16 {
17     for(int j = 1; j * 2 <= cur + 1; j ++)
18     {
19         int equal = 1;
20         _for(k,0,j)
21         if(s[cur-k]!=s[cur-k-j])
22         {
23             equal = 0;
24             break;
25         }
26         if(equal) return 0;
27     }
28     return 1;
29 }
30 int dfs(int cur)
31 {
32     if(cnt++ == n)
33     {
34         _for(i,0,cur)
35         {
36             if(i && (i%64!=0) && (i%4==0)) printf(" ");
37             if(i && (i%64==0)) printf("\n");
38             printf("%c",a[s[i]]);
39         }
40         printf("\n%d\n",cur);
41         return 0;
42     }
43     _for(i,0,L)
44     {
45         s[cur] = i;
46         if(judge(cur))
47             if(!dfs(cur+1))    return 0;
48     }
49     return 1;
50 }
51 
52 int main()
53 {
54     while(cin >> n >> L && (n || L))
55     {
56         memset(s,0,sizeof(s));
57         cnt = 0;
58         dfs(0);
59     }
60     return 0;
61 }

 

posted @ 2018-12-01 17:30  Asurudo  阅读(163)  评论(0编辑  收藏  举报