HDU 5143 DFS

分别给出1,2,3,4   a, b, c,d个

问能否组成数个长度不小于3的等差数列。

首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的

然后直接DFS就行了,但是还是要注意先判合法能否进入下层递归来减少内存消耗。

/** @Date    : 2017-09-27 15:08:23
  * @FileName: HDU 5143 DFS.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

int ans = 0;
bool dfs(int a, int b, int c, int d)
{
	if(ans)
		return 1;
	if(!a && !b && !c && !d)
		return 1;
	else if((a==0 || a > 2) && (b==0 || b > 2) && (c==0 || c > 2) && (d==0 || d > 2))
		return 1;
	else if(a < 0 || b < 0 || c < 0 || d < 0)
		return 0;
	if(!ans)
		ans |= dfs(a - 1, b - 1, c - 1, d - 1);
	if(!ans)
		ans |= dfs(a - 1, b - 1, c - 1, d);
	if(!ans)
		ans |= dfs(a, b - 1, c - 1, d - 1);
	return ans;
}

int main()
{
	/*int size = 256 << 20; // 256MB  
    char *p = (char*)malloc(size) + size;  
    __asm__("movl %0, %%esp
" :: "r"(p));*/
	int T;
	cin >> T;
	while(T--)
	{
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		ans = 0;
		printf("%s
", dfs(a, b, c, d)?"Yes":"No");
	}
    return 0;
}
原文地址:https://www.cnblogs.com/Yumesenya/p/7604561.html