题意:给n个木棍,问能不能正好拼成一个正方形。
解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include<iomanip> #define LL long long #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 using namespace std; int n; int stick[25]; bool vis[25]; bool dfs(int len, int remlen, int pos, int num) { if(remlen == 0) { if(num == 3) return true; return dfs(len, len, 0, num + 1); } for(int i = pos; i < n; i++) { if(i > 0 && !vis[i - 1] && stick[i] == stick[i - 1]) continue; if(!vis[i] && remlen >= stick[i]) { vis[i] = true; if(dfs(len, remlen - stick[i], i + 1, num)) return true; vis[i] = false; } if(!vis[i] && i == 0) return false; } return false; } bool cmp(int a, int b) { return a > b; } int main() { int T; scanf("%d", &T); while(T--) { memset(vis, 0, sizeof vis); scanf("%d", &n); int sum = 0; int maxn = 0; for(int i = 0; i < n; i++) { scanf("%d", &stick[i]); sum += stick[i]; maxn = max(maxn, stick[i]); } sort(stick, stick + n, cmp); if(sum % 4 != 0 || maxn > sum / 4 || n < 4) { puts("no"); continue; } if(dfs(sum / 4, sum / 4, 0, 1)) puts("yes"); else puts("no"); } return 0; }
原文:http://www.cnblogs.com/Apro/p/4902723.html