140 - Bandwidth (UVA)

本文介绍了解决在线编程竞赛中一道关于字符链中找到最小宽度的题目,通过深度优先搜索(DFS)方法,计算每个节点与已确定节点间距离,实现剪枝优化算法。
摘要由CSDN通过智能技术生成

题目链接如下:

Online Judge

这道题我写得太罗嗦了,其实每次dfs深一层的时候,只要算最后加的这个点跟当前已确定的点中、从前往后看最早跟它联结的点之间的距离,大于等于minWidth就可以剪枝。

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cmath>
// #define debug

std::string str, s, result;
char node, ch;
char ans[10];
int minWidth, sz;
std::vector<char> vec[26];
int exist[26];

int calWidth(){
    int len = 1;
    int loc[26];
    for (int i = 0; i < sz; ++i){
        loc[ans[i] - 'A'] = i;
    }
    for (int i = 0; i < 26; ++i){
        for (int j = 0; j < vec[i].size(); ++j){
            int temp = std::abs(loc[i] - loc[vec[i][j] - 'A']);
            if (temp >= minWidth){
                return 10;
            }
            len = std::max(len, temp);
        }
    }
    return len;
}

void dfs(int k){
    if (k == sz){
        int temp = calWidth();
        if (temp < minWidth){
            minWidth = temp;
            result = ans;
        }
        return;
    }
    for (int i = 0; i < sz; ++i){
        int flag = 1;
        for (int j = 0; j < k; ++j){
            if (ans[j] == s[i]){
                flag = 0;
                break;
            }
        }
        if (flag){
            ans[k] = s[i];
            dfs(k + 1);
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, str) && str != "#"){
        for (int i = 0; i < 26; ++i){
            exist[i] = 0;
            vec[i].clear();
        }
        std::stringstream ss(str);
        while (ss >> node){
            exist[node - 'A'] = 1;
            ss >> ch;
            while (ss >> ch){
                if (ch == ';'){
                    break;
                }
                exist[ch - 'A'] = 1;
                vec[node - 'A'].push_back(ch);
            }
        }
        minWidth = 10;
        s.clear();
        for (int i = 0; i < 26; ++i){
            if (exist[i]){
                s.push_back(i + 'A');
            }
        }
        sz = s.size();
        dfs(0);
        for (int i = 0; i < sz; ++i){
            printf("%c ", result[i]);
        }
        printf("-> %d\n", minWidth);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

我写得很麻烦,但我懒得再改了。

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cmath>
// #define debug

std::string str, s, result;
char node, ch;
char ans[10];
int minWidth, sz;
std::vector<char> vec[26];
int exist[26];

int calWidth(){
    int len = 1;
    int loc[26];
    for (int i = 0; i < sz; ++i){
        loc[ans[i] - 'A'] = i;
    }
    for (int i = 0; i < 26; ++i){
        for (int j = 0; j < vec[i].size(); ++j){
            int temp = std::abs(loc[i] - loc[vec[i][j] - 'A']);
            if (temp >= minWidth){
                return 10;
            }
            len = std::max(len, temp);
        }
    }
    return len;
}

void dfs(int k){
    if (k == sz){
        int temp = calWidth();
        if (temp < minWidth){
            minWidth = temp;
            result = ans;
        }
        return;
    }
    for (int i = 0; i < sz; ++i){
        int flag = 1;
        for (int j = 0; j < k; ++j){
            if (ans[j] == s[i]){
                flag = 0;
                break;
            }
        }
        if (flag){
            ans[k] = s[i];
            int loc[26];
            for (int j = 0; j < 26; ++j){
                loc[j] = -1;
            }
            for (int j = 0; j <= k; ++j){
                loc[ans[j] - 'A'] = j;
            }
            int ok = 1;
            for (int j = 0; j < 26; ++j){
                for (int l = 0; l < vec[j].size(); ++l){
                    if (loc[j] != -1 && loc[vec[j][l] - 'A'] != -1 && std::abs(loc[j] - loc[vec[j][l] - 'A']) >= minWidth){
                        ok = 0;
                        j = 26;
                        break;
                    }
                }
            }
            if (ok){
                dfs(k + 1);
            }
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, str) && str != "#"){
        for (int i = 0; i < 26; ++i){
            exist[i] = 0;
            vec[i].clear();
        }
        std::stringstream ss(str);
        while (ss >> node){
            exist[node - 'A'] = 1;
            ss >> ch;
            while (ss >> ch){
                if (ch == ';'){
                    break;
                }
                exist[ch - 'A'] = 1;
                vec[node - 'A'].push_back(ch);
            }
        }
        minWidth = 10;
        s.clear();
        for (int i = 0; i < 26; ++i){
            if (exist[i]){
                s.push_back(i + 'A');
            }
        }
        sz = s.size();
        dfs(0);
        for (int i = 0; i < sz; ++i){
            printf("%c ", result[i]);
        }
        printf("-> %d\n", minWidth);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值