poj 2363 poj 1011 dfs + 剪枝
来源:互联网 发布:淘宝好评语 编辑:程序博客网 时间:2024/05/29 16:07
#include <stdio.h>#include <stdlib.h>#include <string.h>int sticks[20];bool visit[20];int cmp(const void * a,const void *b){return *(int *)b - *(int *)a;}bool dfs(int s,int N,int cur,int left,int num,int lengthside) // num 是已经拼好的 长度{ //s 是搜索木棍的起始位置 N 是木棍的总数 left是要拼凑的长度 cur是当前的长度if (num==3) //找到三根 其他的就不用找了return true;int i;for (i=s;i<N;i++){if (!visit[i]){if (cur + sticks[i] == lengthside) // 刚好拼完一根{visit[i] = true;if (dfs(0,N,0,lengthside,num+1,lengthside))return true;visit[i] = false;}else if (cur + sticks[i] < lengthside){visit[i] = true;if (dfs(i+1,N,cur+sticks[i],left - sticks[i],num,lengthside))return true;visit[i] = false;}}}return false;}int main(){int test,N,i,sum,max,lengthside;scanf("%d",&test);while(test--){scanf("%d",&N);sum = 0;max = 0;for (i=0;i<N;i++){scanf("%d",&sticks[i]);sum += sticks[i];if (max < sticks[i])max = sticks[i];}if ( sum % 4 != 0 || sum / max <4) //基本剪枝 {printf("no\n");continue;}memset(visit,false,sizeof(visit));qsort(sticks,N,sizeof(int),cmp);lengthside = sum / 4;if (dfs(0,N,0,lengthside,0,lengthside))printf("yes\n");elseprintf("no\n");}return 0;}
两题基本一样
先对木棍排序 ,然后按照质指定的长度在数组里搜索。
刚开始,这两题都是按照子集和的方法做的,一直TLE,无论怎么剪枝都不行,后来一想,还是按照指定长度搜索正解效率更高。
- poj 2363 poj 1011 dfs + 剪枝
- POJ 1011 (DFS+剪枝)
- poj 1011 dfs剪枝
- poj 1011Sticks(DFS +剪枝)
- POJ 1011 Sticks dfs + 剪枝
- DFS 剪枝1 poj 1011
- POJ 1011 Sticks DFS+剪枝
- poj 1011 Sticks -----dfs+剪枝
- POJ 1011 Sticks (DFS + 剪枝)
- POJ 1011 Sticks DFS 剪枝
- poj 1011 Sticks 【DFS】+【剪枝】
- [POJ 1011]Sticks(DFS剪枝)
- poj 1011 :Sticks (dfs+剪枝)
- POJ 1011 Sticks dfs+剪枝
- poj 1011 Sticks(dfs+剪枝)
- POJ 1011 Sticks DFS+剪枝
- POJ 1011 sticks DFS+剪枝
- poj 1011 Sticks (DFS剪枝)
- 字符集(Character Sets)
- 3D 常用公式
- Android中LayoutParams的用法
- UDP内网穿透讲解及代码实现
- 如何在GitHub上协作开发开源项目?
- poj 2363 poj 1011 dfs + 剪枝
- Java Programming Tutorial
- Android Studio会取代Eclipse吗?
- 学习java杂记
- IOCP程序
- Linux入门->常用设备名称
- 八个SVN提交的好习惯
- Minigui学习--画不同图形
- OCP-1Z0-052-V8.02-91题