dfs

本文解析了三道经典的编程题目,包括凑零钱问题、滑冰场布局问题及朋友关系问题,采用DFS深度优先搜索算法并结合剪枝策略解决。

1、团队程序设计天梯赛-练习集-L3-001 凑零钱

参考:http://blog.youkuaiyun.com/chan_yeol/article/details/51362182

解题思路:

dfs + 剪枝

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e4 + 10;
int N, M;
int num[maxn];
int sum = 0;
int ans[maxn];
int Count = 0;

bool dfs(int sum, int pos, int leave);

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    scanf("%d%d", &N, &M);
    for (int i = 0; i < N; ++i) {
        scanf("%d", &num[i]);
        sum += num[i];
    }
    sort(num, num+N);
    bool flag = false;
    for (int i = 0; i < N; ++i) {
        if (dfs(num[i], i, sum)) {
            flag = true;
            printf("%d", num[i]);
            for (int j = Count-1; j >= 0; --j) {
                printf(" %d", ans[j]);
            }
            printf("\n");
            break;
        }
    }
    if (!flag) {
        printf("No Solution\n");
    }
    return 0;
}

bool dfs(int sum, int pos, int leave)
{
    if (sum > M || num[pos] > M) {
        return false;
    }
    if (sum == M) {
        return true;
    }
    leave -= num[pos];
    for (int i = pos+1; i < N; ++i) {
        if (sum+num[i] > M || leave+sum < M) {  //剩下的不足时不再进行搜索
            return false;
        }
        if (sum+num[i] == M) {
            ans[Count++] = num[i];
            return true;
        }
        if (dfs(sum+num[i], i, leave)) {
            ans[Count++] = num[i];
            return true;
        }
    }
    return false;
}

2、Codeforces 217A Ice Skating

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>

using namespace std;

#define lson low, mid, _id<<1
#define rson mid + 1, high, _id<<1|1

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;

const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;

int n, cnt = 0;
int x[maxn], y[maxn];
bool vis[maxn];

void dfs(int a, int b);

int main() {
#ifdef Floyd
    freopen("in.txt", "r", stdin);
#endif
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &x[i], &y[i]);
    }
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < n; ++i) {
        if (!vis[i]) {
            ++cnt;
            dfs(x[i], y[i]);
        }
    }
    printf("%d\n", cnt - 1);
    return 0;
}

void dfs(int a, int b) {
    for (int i = 0; i < n; ++i) {
        if (!vis[i] && (a == x[i] || b == y[i])) {
            vis[i] = true;
            dfs(x[i], y[i]);
        }
    }
}

3、HDU 5305 Friends

参考:http://blog.youkuaiyun.com/yeguxin/article/details/47042115

解题思路:

dfs枚举边的状态,直接枚举到m条边后再判断会TLE

加判断剪枝后,若枚举的边达到m条即是一种解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x7fffffff;
int T, n, m, x, y, ans = 0;
int key[10], Left[30], Right[30], key_a[10], key_b[10];
bool flag = true;

void dfs(int cnt) {
    if (cnt == m) { ++ans; return; }
    int l = Left[cnt], r = Right[cnt];
    if (key_a[l] < (key[l] / 2) && key_a[r] < (key[r] / 2)) {
        ++key_a[l]; ++key_a[r]; dfs(cnt + 1); --key_a[l]; --key_a[r];
    }
    if (key_b[l] < (key[l] / 2) && key_b[r] < (key[r] / 2)) {
        ++key_b[l]; ++key_b[r]; dfs(cnt + 1); --key_b[l]; --key_b[r];
    }
}

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_H
    scanf("%d", &T);
    while (T--) {
        scanf("%d %d", &n, &m);
        memset(key, 0, sizeof(key));
        REP(i, m) { scanf("%d %d", &x, &y); Left[i] = x; Right[i] = y; ++key[x]; ++key[y]; }
        flag = true;
        for (int i = 1; i <= n; ++i) {
            if (key[i] % 2 != 0) { flag = false; break; }
        }
        if (!flag) { printf("0\n"); continue; }
        memset(key_a, 0, sizeof(key_a)); memset(key_b, 0, sizeof(key_b));
        ans = 0; dfs(0); printf("%d\n", ans);
    }
#ifdef __AiR_H
    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
    return 0;
}

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值