UVa 140 - Bandwidth(BFS剪枝/未)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=76

算是体会到了剪枝,不是生成完全后再判断,即不是cur==n再判断,而是在新加入元素时就判断,若是不能直接后面的都不再加了。

WA

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <cmath>
  5 #include <algorithm>
  6 using namespace std;
  7 int mn,n;
  8 int ch[8][8];
  9 int flg[8];
 10 int vis[8];
 11 string res;
 12 void dfs(int cur,string str)
 13 {
 14     if(cur==n)
 15     {
 16         int s=str.length();
 17         int s1=-1;
 18         for(int j=0; j<s; j++)
 19         {
 20             if(ch[str[s-1]-'A'][str[j]-'A']==1)
 21             {
 22                 if(mn<=s-j) return;
 23                 if(s1<s-j)s1=s-j;
 24             }
 25         }
 26         if(s1>0)
 27         {
 28             mn=s1;
 29             res=str;
 30             // cout<<str<<"\n";
 31         }
 32         return;
 33     }
 34     for(int i=0; i<8; i++)
 35     {
 36         if(flg[i]&&!vis[i])
 37         {
 38             string cp=str;
 39             cp+='A'+i;
 40             for(int j=0; j<str.length(); j++)
 41             {
 42                 if(ch[i][str[j]-'A']==1)
 43                 {
 44                     if(mn<=str.length()-j) return;
 45                 }
 46             }
 47             vis[i]=1;
 48             dfs(cur+1,cp);
 49             vis[i]=0;
 50         }
 51     }
 52 }
 53 int main()
 54 {
 55     string str;
 56     while(cin>>str&&str[0]!='#')
 57     {
 58         n=0;
 59         int l=str.length();
 60         for(int i=0; i<l; i++)
 61         {
 62             if(str[i]>='A'&&str[i]<='Z')
 63             {
 64                 if(!flg[str[i]-'A'])
 65                 {
 66                     flg[str[i]-'A']=1;
 67                     n++;
 68                 }
 69             }
 70         }
 71         // cout<<"n:"<<n<<"\n";
 72         mn=1e5;
 73         int k=0,cur;
 74         for(int i=0; i<l; i++)
 75         {
 76             if(str[i]==':')
 77             {
 78                 k=1;
 79             }
 80             else if(str[i]==';')
 81             {
 82                 k=0;
 83             }
 84             else if(k)
 85             {
 86                 ch[cur][str[i]-'A']=1;
 87                 ch[str[i]-'A'][cur]=1;
 88             }
 89             else
 90             {
 91                 cur=str[i]-'A';
 92             }
 93         }
 94         dfs(0,"");
 95         for(int i=0; i<n; i++)
 96         {
 97             cout<<res[i]<<" ";
 98         }
 99         printf("-> %d\n",mn);
100         /* for(int i=0; i<8; i++)
101          {
102              for(int j=0; j<8; j++)
103              {
104                  printf("%d ",ch[i][j]);
105              }
106              printf("\n");
107          }
108          */
109         for(int i=0; i<8; i++)
110         {
111             vis[i]=flg[i]=0;
112             for(int j=0; j<8; j++)
113             {
114                 ch[i][j]=0;
115             }
116         }
117     }
118     return 0;
119 }

 

posted @ 2016-03-17 11:34  cherry_yue  阅读(44)  评论(0)    收藏  举报