天天看点

HDU5142 NPY and arithmetic progression && BestCoder Round #23 1002

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143

解题思路:BestCoder官方题解:

可以发现等差数列只有(123,234,1234和长度>=3的常数列),如果选择非常数列(123,234,1234)数量大于等于3,可以变为三个或4个常数列,例如(123,123,123)变为(111,222,333)。所以从0-2枚举选择非常数列的数量,再判断能否用常数列覆盖剩下的(如果数字长度正好为0或
  
   ≤3
  就可以)。或者大数就当成20左右的,然后记忆化搜索      
#include<iostream>
#include<cstdio>
using namespace std;

int dfs(int a,int b,int c,int d)
{
    if(a+b+c+d==0)
        return 1;
    if((a==0||a>=3)&&(b==0||b>=3)&&(c==0||c>=3)&&(d==0||d>=3))
        return 1;
    if(a>=1&&b>=1&&c>=1&&d>=1)
        if(dfs(a-1,b-1,c-1,d-1))
            return 1;
    if(a>=1&&b>=1&&c>=1)
        if(dfs(a-1,b-1,c-1,d))
            return 1;
    if(b>=1&&c>=1&&d>=1)
        if(dfs(a,b-1,c-1,d-1))
            return 1;
    return 0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int a,b,c,d;
        int i;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        if(dfs(a,b,c,d))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}