首页 > 其他分享 >poj-2362

poj-2362

时间:2023-05-23 16:05:04浏览次数:34  
标签:sticks edgeMaxLength int long poj dfs 2362 stick


//132K  141MS   C++ with beginSearchPos
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int cmp(const void* a,const void* b)  //降序  
{  
    return *(long long*)b-*(long long*)a;  
}

long long fourEdgeLength[4];

long long lengthSum;
long long edgeMaxLength;

int caseNum;

bool flag;

long long sticks[21];
int stickNum;

char visited[21];

void dfs(int completeLineNum, int leftLength, int beginSearchPos) {
    if (flag) {
        return;
    }

    if (completeLineNum == 3) {
        flag = 1;
        return;
    }

    for (int i = beginSearchPos; i <= stickNum; i++) {
        if (!visited[i]) {
            visited[i] = 1;
            if (sticks[i] == leftLength) {
                completeLineNum++;
                if (completeLineNum == 3) {
                    flag = 1;
                    return;
                } else {
                    dfs(completeLineNum, edgeMaxLength, 1);
                }
            } else if (sticks[i] < leftLength) {
                dfs(completeLineNum, leftLength - sticks[i], i + 1);
            }
            visited[i] = 0;
            if (flag) {
                return;
            }
        }
    }
}

int main() {
    scanf("%d", &caseNum);
    for (int i = 1; i <= caseNum; i++) {
        flag = 0;
        lengthSum = 0;
        memset(sticks, 0, sizeof(sticks));
        memset(visited, 0, sizeof(visited));
        scanf("%d", &stickNum);
        memset(fourEdgeLength, 0, sizeof(fourEdgeLength));
        int MAX = -1;
        for (int j = 1; j <= stickNum; j++) {
            scanf("%lld", &sticks[j]);
            lengthSum += sticks[j];
            MAX = MAX > sticks[j] ? MAX: sticks[j];
        }
        qsort(sticks, stickNum , sizeof(long long), cmp);
        edgeMaxLength = lengthSum/4;
        if (lengthSum%4 || sticks[0] > edgeMaxLength) {
            printf("no\n");
            continue;
        }

        // printf("edgeMaxLength %lld\n", edgeMaxLength);
        dfs(0, edgeMaxLength, 1);
        if (flag) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
}


经典的dfs剪枝题,一般一道题如果使用DFS做的话,都会多少的考察剪枝, 这里将stick预先从大到小排序是关键,这样在后面每次dfs时 不必遍历所有的stick遍历,

只需遍历边上一次选择的stick短的即可(很重要,每次都从1开始遍历stick会TLE)

标签:sticks,edgeMaxLength,int,long,poj,dfs,2362,stick
From: https://blog.51cto.com/u_9420214/6332964

相关文章

  • poj-2371
    //524K63MSC++#include<cstdio>#include<cstring>#include<cstdlib>intcmp(constvoid*a,constvoid*b){return*((int*)a)-*((int*)b);}usingnamespacestd;constintMAX=100001;intarray[MAX];intdataBaseSiz......
  • poj-1930
    //144K0MSC++#include<cstdio>#include<cstring>#include<cmath>intgcd(inta,intb){if(b==0){returna;}elseif(a>b){returngcd(b,a%b);}else{returngcd(a,b%a);}}/......
  • poj-1023
    //184K0MSC++#include<cstdio>#include<cstring>usingnamespacestd;charNP[65];//-1:n,1:pcharstr[80];chardigitUsed[80];charbinaryExpression[80];intcaseNum;intlength;longlongval;voidsolve(longlongval){l......
  • poj-1401
    //408K375MSG++#include<cstdio>#include<cstring>longlongget2FactorNum(longlongN){longlongres=0;while(N){res+=N/2;N/=2;}returnres;}longlongget5FactorNum(longlongN){long......
  • poj-2231
    //264K 47MS C++#include<cstdio>#include<cstring>#include<cstdlib>constintMAX=10005;longlongcowLocation[10005];intcmp(constvoid*a,constvoid*b){ return*((longlong*)a)-*((longlong*)b);}longlongcowNum;......
  • poj-1308
    //392K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=10000;intUF_set[MAX];voidUF_get_setId(intcurId){intparentId=UF_set[curId];if(parentId==0){return;}while(UF......
  • poj-1120
    //408K16MSG++#include<cstdio>#include<cstring>usingnamespacestd;intdish[20][20];intK[20][20];intD[16];intdays;intgetDensitySum(introwId,intcolumnId){intK=0;K+=dish[rowId][columnId];if(rowId......
  • poj-3641
    //712K0MSG++#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>usingnamespacestd;longlonga,p;//longlongpower2(longlonga,longlongn)//{//longlongret=1;//for(longlongm......
  • poj-1026
    //188K110MSC++#include<cstring>#include<cstdio>#include<iostream>usingnamespacestd;charstr1[205];charstr2[205];intkey[205];intcycleLength[205];//voidreplace(char*str,intkeyLength,intstrLength){//......
  • poj-2707
    //408K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;intoX;intoY;intdX;intdY;inlinedoubleMIN(doublea,doubleb){returna<b?a:b;}inlinedoubleMAX(doublea,doubleb){returna>b?......