hdu 1560(IDA*)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560

思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较,如果相同,这个字符串的长度减一。构造h()为当前所有字符串中长度最长的。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 char map[10][10];
 8 char str[10]="ACGT";
 9 int len[10];
10 int n,maxdeep;
11 
12 bool Judge()
13 {
14     int maxlen=0;
15     for(int i=0;i<n;i++)maxlen=max(maxlen,len[i]);
16     return maxlen==0;
17 }
18 
19 int Get_H()
20 {
21     int maxlen=0;
22     for(int i=0;i<n;i++)maxlen=max(maxlen,len[i]);
23     return maxlen;
24 }
25 
26 bool IDA_star(int deep)
27 {
28     if(deep==maxdeep)return Judge();
29     if(Get_H()+deep>maxdeep)return false;
30 
31     int tmp_len[10];
32     for(int i=0;i<4;i++){
33         bool flag=false;
34         for(int j=0;j<n;j++){
35             tmp_len[j]=len[j];
36             if(len[j]==0)continue;
37             if(map[j][len[j]-1]==str[i]){
38                 len[j]--;
39                 flag=true;
40             }
41         }
42         if(flag){
43             if(IDA_star(deep+1))return true;
44         }
45         for(int j=0;j<n;j++)len[j]=tmp_len[j];
46     }
47     return false;
48 }
49 
50 
51 int main()
52 {
53     int _case;
54     scanf("%d",&_case);
55     while(_case--){
56         scanf("%d",&n);
57         for(int i=0;i<n;i++){
58             scanf("%s",map[i]);
59             len[i]=strlen(map[i]);
60         }
61         for(maxdeep=1; ;maxdeep++){
62             if(IDA_star(0))break;
63         }
64         printf("%d\n",maxdeep);
65     }
66     return 0;
67 }
View Code

 

posted @ 2013-09-17 21:07  ihge2k  阅读(692)  评论(0编辑  收藏  举报