hdoj 1518 Square 【dfs】

题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形。

策略:深搜。

hdoj 1455的简化版

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 25
using namespace std;
int s[M], n, ans;//ans就是答案
bool vis[M];
int dfs(int cou, int cur, int pos){  //cou是已分配的木棍数,cur是当前的长度, pos是当前的序号
	if(cou == n){
		return 1;
	}
	int i;
	for(i = pos; i < n; i ++){
		if(vis[i]) continue;
		if(cur+s[i] < ans){
			vis[i] = 1;
			if(dfs(cou+1, cur+s[i], i+1)) return 1;
			vis[i] = 0;
			if(cur == 0) return 0;
			while(s[i] == s[i+1]&&i+1<n) ++i;
		}
		else if(cur+s[i] == ans){
			vis[i] = 1;
			if(dfs(cou+1, 0, 0)) return 1;
			vis[i] = 0;
			return 0;
		}
	}
	return 0;
}
int main(){
	int t, i;
	scanf("%d", &t);
	while(t --){
		int sum = 0;
		scanf("%d", &n);
		for(i = 0; i < n; i ++){
			scanf("%d", &s[i]);
			sum+=s[i];
		}
		if(sum%4){  //假设能,肯定能整除4
			printf("no\n");
			continue;
		}
		ans = sum/4;
		memset(vis, 0, sizeof(vis));
		int flag = dfs(0, 0, 0);
		printf("%s\n", flag?

"yes":"no");
	}
	return 0;
}

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

时间: 2024-08-26 20:39:41

hdoj 1518 Square 【dfs】的相关文章

hdoj 1455 Sticks 【dfs】

题意:找最短的木棍能够组成的长度, hdoj  1518 的加强版 代码: #include <stdio.h> #include <string.h> #include <algorithm> using std::sort; #define M 70 int s[M], vis[M]; int n, ans; int cmp(int a, int b) { return a > b; } int dfs(int cou, int cur, int pos) {

hdoj 1015 Safecracker【DFS】

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10612    Accepted Submission(s): 5429 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle

hdoj 1342 Lotto【dfs】

Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1736    Accepted Submission(s): 854 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49}

hdoj 1045 Fire Net 【DFS】

题意:如果两个点要放在同一行或者同一列,那么两个点中间要有一个墙,否则的话只能放一个点,最后问你最多能放几个点. 看了一个星期.. 这道题的解法我还是第一次见,就是逐个逐个的来放置每个点,然后每经过一个点都判断一次,详情看代码 代码: #include <stdio.h> #include <string.h> int ans, n; char map[10][10]; int judge(int lin, int row) { int i; for(i = lin-1; i &g

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

hdoj 2553 N皇后问题 【DFS】

题意:... 典型的深搜,就是处理对角线的时候有些意外. 代码(注释掉的就是深搜,因为我不打表的话 TL): #include<stdio.h> int c[11], n, ans; int res[10] = {1, 0, 0, 2, 10, 4, 40, 92, 352, 724}; /*void dfs(int cur) { if(cur == n) { ++ans; return ; } int i, j, flag; for(i = 0; i < n; i ++){ flag

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

NYOJ 587 blockhouses 【DFS】

blockhouses 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle th

HDU1045 Fire Net 【DFS】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6285    Accepted Submission(s): 3552 Problem Description Suppose that we have a square city with straight streets. A map of a city is a