DFS questions and brief analysis [continuously brushing questions, updating]

2018.4.20

UVA572 Oilfield After reading the examples in Mr. Liu Rujia's purple book, an algorithm called flood filling or seed filling should be used.

4.21

HDU1455 sticks  use the idea of ​​pruning

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100;
int a[maxn],v[maxn];
int p;//Determine whether the minimum value is calculated
int m;//Number of sticks m input

int cmp(int i,int j)
{
    return i>j;
}
void dfs(int beg, int len, int aim, int num)//Start subscript, stick length, target length, total number of spliced ​​sticks
{
    if(p)return;
    if(num == m){
        p = 1;
        return;
    }
    for(int i = beg; i < m; i ++)
    {
        if(a[i] + len <= aim && v[i] != 1)
        {
            v [i] = 1;
            if(a[i]+len == aim)dfs(0,0,aim,num+1);
            else{
                dfs(i + 1, a[i] + len, aim, num+1);//This is a place where num++ is used for timeout and ++num is used for WA
            }
            v [i] = 0;
            if(p) return;//Complete, return
            if(len == 0)return;//Pruning, if the former does not meet the conditions, then the latter must also not meet
            while(i < m && a[i+1] == a[i]) ++i;//Pruning, if the condition is not met, skip the sticks of equal length that do not satisfy the condition
        }
    }
}

intmain()
{
    int sum;//Total length of all sticks
    while(cin >> m && m!= 0)
    {
        memset(v,0,sizeof(v));
        sum = 0;
        for(int i = 0; i < m; i ++)
        {
            cin >> a[i];
            sum += a[i];
        }
        sort(a,a+m,cmp);
        for(int i = a[0]; i <= sum; i ++)
        {
            if(i == sum)cout << sum << endl;//If there is only one stick, output directly
            else if(sum % i == 0)//prune
            {
                p = 0;
                dfs(0,0,i,0);
                if(p)
                {
                    cout << i << endl;
                    break;
                }
            }
        }
    }
    return 0;
}

4.23

HDU2553 N Queen problem  is timed out anyway, and I hit the watch in a fit of rage

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
using namespace std;

int tot, n;
int v[20][20];

void searchs(int r)
{
    if(r >= n)
    {
         to ++;
         return;
    }
    for(int i = 0; i < n; i ++)
    {
        if(!v[0][i]&&!v[1][r+i]&&!v[2][r-i+n])
        {
            v [0] [i] = v [1] [r + i] = v [2] [r-i + n] = 1;
            searchs(r+1);
            v [0] [i] = v [1] [r + i] = v [2] [r-i + n] = 0;
        }
    }
}

intmain()
{
    while(scanf("%d",&n)&&n)
    {
//        clock_t start,ends;
//        start = clock();
        to = 0;
        memset(v,0,sizeof(v));
        searchs(0);
        printf("%d\n",tot);
//        ends = clock();
//        printf("time:%d\n",ends - start);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730190&siteId=291194637