NYOJ 927 The partial sum problem 【DFS】+【剪枝】
The partial sum problem
- 描写叙述
-
One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K.
- 输入
-
There are multiple test cases.
Each test case contains three lines.The first line is an integer N(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8). - 输出
- If Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.
- 例子输入
-
4 1 2 4 7 13 4 1 2 4 7 15
- 例子输出
-
Of course,I can! Sorry,I can't!
这题非常经典,剪枝的时候要细心。
#include <stdio.h>
#include <stdlib.h>
int n, arr[22], sum, vis[22], ok, count;
const char *sam[] = {"Sorry,I can't!\n", "Of course,I can!\n"};int cmp(const void *a, const void *b){return *(int *)a - *(int *)b;
}void DFS(int k){if(count == sum){ok = 1; return;}for(int i = k; i < n; ++i){if(i && arr[i] == arr[i-1] && !vis[i-1]) //cutcontinue;if(count > sum && arr[i] > 0) return; //cutcount += arr[i]; vis[i] = 1;DFS(i + 1);if(ok) return;count -= arr[i]; vis[i] = 0;}
}int main(){while(scanf("%d", &n) == 1){for(int i = 0; i < n; ++i){scanf("%d", arr + i);vis[i] = 0;}scanf("%d", &sum);qsort(arr, n, sizeof(int), cmp);count = ok = 0; DFS(0);printf(ok ? sam[1] : sam[0]);}return 0;
}
NYOJ 927 The partial sum problem 【DFS】+【剪枝】相关推荐
- NYOJ--927--dfs--The partial sum problem
/*Name: NYOJ--927--The partial sum problemAuthor: shen_渊 Date: 15/04/17 19:41Description: DFS,和 NYOJ ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- poj2362 DFS+剪枝
题大致做法就是对所有小棒子长度求和sum,sum就是正方形的周长,sum/4就是边长side. 问题就转变为:这堆小棒子能否刚好组合成为4根长度均为side的大棒子 不难了解,小棒子的长度越长,其灵活 ...
- [Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]
[问题描述][中等] [解答思路] 1. 回溯 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.De ...
- 【HDU - 1518】Square (经典的dfs + 剪枝)
题干: Given a set of sticks of various lengths, is it possible to join them end-to-end to form a squar ...
- poj2248 DFS+剪枝 or BFS
传送门 2248:Addition Chains 描述 An addition chain for n is an integer sequence with the following four p ...
- DFS(剪枝与优化) - 洛谷 P1361 - 小猫爬山
DFS(剪枝与优化) - 洛谷 P1361 - 小猫爬山 翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_&l ...
- 牛客算法周周练17D - 刺客信条(dfs剪枝 | 优先队列 + bfs)
题目大意: 给出一张n * m的地图,每经过一个建筑都需要一定的时间,而经过ABC要花费100,要求输出从S到E的最短时间. 解题思路: 思路一:dfs剪枝 输入地图的时候用char输入,然后转成in ...
- DFS剪枝优化 小猫爬山 数独
DFS剪枝策略总结 优化搜索顺序 优先搜索分支数少(剩余选择少)的情况 排除等效冗余 若对顺序没有要求 可以将排列转化为组合 可行性剪枝 不合法的情况不进行搜索 最优化剪枝 若当前的"消耗& ...
最新文章
- “众声喧哗”中的VR,谁来买单?
- 2022.3.28紫薇
- 使用Github(目的、基本你概念、注册账号)
- CentOS 7.4下使用yum安装MySQL5.7.20 最简单的
- Linux下添加普通帐号
- button的图文设置
- MATLAB偏振光的反射与折射,仿真程序动画作品--光的反射与折射*
- JavaScript、Ajax、jQuery全部知识点,5分钟速懂!
- Unity3D的四种坐标系
- pip不是内部命令问题
- JavaSE JDK搭建
- 通过IDA Pro生成asm文件
- 楼宇跨界智能家居未来前景如何有待考证
- 以收音机为例总结分析硬件电路的三个步骤
- Load Switch负载开关详解
- python灰色关联度分析代码_GRA灰色关联度分析学习 附python代码
- 白山数聚蜂巢敏捷集成助力能源企业数字化转型
- Linux man指令查询文档设定成中文
- 新买的电脑网速慢的解决方案
- myeclispe 启动报错Location	Type Faceted project metadata file /farming/.settings/org.eclips
热门文章
- python中进制chr_python中的chr() 如何返回字符?
- 机器人学习--F1TENTH弗吉尼亚大学无人驾驶课程
- Python IDLE 如何清屏
- Xilinx Platform Cable USB II 下载器驱动安装教程——Win10
- Bentley MicroStation V8i安装教程
- 【ML】 李宏毅机器学习一:Regression、Gradient Descent(python实现)
- VC/MFC如何设置对话框背景颜色
- 正则化的通俗解释_干货|深度学习中的正则化技术概述(附Python+keras实现代码)...
- 咖啡html代码,HTML5咖啡生成代码动画
- 【机器学习】深入理解CatBoost