Poj Square take two brush * DFS pruning issue is very important

The basic idea:

Collapsed, there are two main problems:

1. there is a problem pruning, pruning will not time out;

 

2.DFS wording of the question, I feel like OJ in particular by returning true false, to multi-loop recursive solve all problems;

 

bool dfs(int n,int index,int value,int num) {
    if (num == 3)
        return true;
    for (int i = index; i < n; i++) {
        if (!vis[i]) {
            vis[i] = true;
            if (vec[i] + value == cnt) {
                if (dfs(n, 0, 0, num+1))
                    return true;
            }
            else if (vec[i] + value < cnt) {
                if (dfs(n, i+1, vec[i] + value, num))
                    return true;
            }
            vis[i] = false;
        }
    }
    return false;
}

 In the above code, for example, by entering recursive All results obtained;

First of all sorts, from large to ensure that the election since so little of the election does not occur when a large finish can not be adapted, this time himself could not see out;

Overall, is calculated through three iterations, each iteration be found by recursively traverse the condition is met;

At that time he stuck in how to link these three iterations, using their own for loop cycle three times, or timed out;

 

Big brother code when the first through satisfied directly set into the new layer of recursive iteration, and since vis array does not change, it can be seen again continue to search for the previous base;

 

 Secondly, the number of iterations to monitor num, num = 3 when the time beginning on direct return true;

Subsequent overall can be correctly determined whether the reaction to the first layer by return, so it will know whether to find is successful. DFS can be seen as an upgraded version of the former, we need stratification;

 

key point:

Pruning problem;

 

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

const int maxn = 22;
bool vis[maxn];
vector<int>vec;
int cnt;//边长

void init(int n) {
    fill(vis, vis + n, false);
    vec.resize(n);
    //correct = 0;
    cnt = 0;
}

bool cmp(int a, int b) {
    return a > b;
}

bool dfs(int n,int index,int value,int num) {
    if (num == 3)
        return true;
    for (int i = index; i < n; i++) {
        if (!vis[i]) {
            vis[i] = true;
            if (vec[i] + value == cnt) {
                if (dfs(n, 0, 0, num+1))
                    return true;
            }
            else if (vec[i] + value < cnt) {
                if (dfs(n, i+1, vec[i] + value, num))
                    return true;
            }
            vis[i] = false;
        }
    }
    return false;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int m;
        cin >> m;
        init(m);
        for (int i = 0; i < m; i++) {
            cin >> vec[i];
            cnt += vec[i];
        }
        if (cnt % 4 != 0) {
            cout << "no" << endl;
            continue;
        }
        cnt /= 4;
        sort(vec.begin(), vec.end(), cmp);
        if (vec[0] > cnt) {
            cout << "no" << endl;
            continue;
        }
        //dfs(n, 0, vec[0]);
        if (dfs(m,0,0,0))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/songlinxuan/p/12446189.html