symons

___________每一天都是幸福的!!

  博客园  ::  :: 新随笔  ::  :: 订阅 订阅  :: 管理
 1 /*
 2  *  dfs 
 3  *    esc <------> capslock
 4  */
 5 
 6 #include <iostream>
 7 #include <string>
 8 #include <algorithm>
 9 #include <cstdio>
10 using namespace std;
11 int num[20],n;
12 int l;
13 bool success;
14 string haha;
15 bool vit[20];
16 bool cmp(int a,int b){
17     return a>b;
18 }
19 
20 void init(string str){
21     int i,j;
22     l=str.length();
23     success=0;
24     for(i=0;i<l;++i){
25         num[i]=str[i]-'A'+1;
26         vit[i]=0;
27     }
28     sort(num,num+l,cmp);
29     return ;
30 }
31 int count(string strr){
32     int i,cnt=0;
33     int str[5];
34     for(i=0;i<5;++i)    str[i]=strr[i]-'A'+1;
35     cnt=str[0]-str[1]*str[1]+str[2]*str[2]*str[2]-str[3]*str[3]*str[3]*str[3]+str[4]*str[4]*str[4]*str[4]*str[4];
36     return cnt;
37 }
38 void dfs(int step,string res){
39     int i,j;
40     int temp;
41     string str="0";
42     if(success) return ;
43     for(i=0;i<l;++i){
44         str="0";
45         if(vit[i]==1) continue;
46         if(res=="0") str[0]=num[i]+'A'-1;
47         else{
48             str[0]=num[i]+'A'-1;
49             str=res+str;
50         }
51         if(step==1) {
52             vit[i]=1;
53             dfs(2,str);
54             vit[i]=0;
55         }else if(step==2){
56             vit[i]=1;
57             dfs(3,str);
58             vit[i]=0;
59         }else if(step==3){
60             vit[i]=1;
61             dfs(4,str);
62             vit[i]=0;
63         }else if(step==4){
64             vit[i]=1;
65             dfs(5,str);
66             vit[i]=0;
67         }else if(step==5){
68 //            cout<<str<<endl;
69             temp=count(str);
70             if(temp==n) {haha=str;success=1;return;}
71         }
72     }
73     return ;
74 }
75 
76 
77 int main(){
78     int i,j,t;
79     string str;
80     while(cin>>n>>str){
81         if(n==0&&str=="END") break;
82         t=0;
83         init(str);
84         dfs(1,"0");
85         if(success==1){
86             cout<<haha<<endl;
87         }else cout<<"no solution\n";
88     }
89     return 0;
90 }
91 
92 
93         

 

posted on 2013-09-20 16:47  symons  阅读(187)  评论(0编辑  收藏  举报