uva 12166 bfs

来源:互联网 发布:linux yum 安装 svn 编辑:程序博客网 时间:2024/06/18 05:16

这一题与白书上的一题关于天平的题目有些相似
uva839

#include <iostream>#include <cstdio>#include <cstring>using namespace std;bool solve(int& w){    int w1,w2,d1,d2;    cin >> w1 >> d1 >> w2 >> d2;    bool b1 = true,b2 = true;    if(!w1) b1 = solve(w1);    if(!w2) b2 = solve(w2);    w = w1 + w2;    return b1 && b2 && (w1*d1==w2*d2);}int main(){    int T,w;    cin >> T;    while(T--){        if(solve(w)) cout<<"YES\n";else cout<<"NO\n";        if(T) cout<<"\n";     }    return 0;} 

这一题一开始并不怎么会做,是看别人结题报告的。
在这里结题报告
里面关于算法讲解的十分清楚
下面是代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <map> #include <algorithm>using namespace std;string line;map<long long,int> dic;int sum;void dfs(int depth,int s,int e){    if(line[s] == '['){        int pos = 0;        for(int i = s+1 ; i < e;i++){            if(line[i] == '[') pos++;            if(line[i] == ']') pos--;            if(pos==0 && line[i]==','){                dfs(depth+1,s+1,i-1);                dfs(depth+1,i+1,e-1);            }        }    }    else{        long long w = 0;        for(int i=s;i<=e;i++) w=w*10 + line[i]-'0';        sum++;        dic[w<<depth]++;    }}int main(){    int T;    cin >> T;    while(T--){        dic.clear();        sum = 0;        cin >> line;        dfs(0,0,line.size()-1);        int maxn = 0;        for(map<long long,int>::iterator it = dic.begin(); it != dic.end(); ++it) maxn = max(maxn, it->second);          cout<<sum-maxn<<endl;    }} 
1 0
原创粉丝点击