Sum It Up -- 深搜 ---较难


每一行都是一组测试案例 第一个数字 表示总和 第二个数字表示 一共有几个可用数据 现在 按照从小到大的顺序 输出 那些数字中若干数字之和为总和的 信息 /.
很好很明显的 遍历痕迹 , 多看多练
// 利用vector不定长数组 构图 然后就知道 某个节点相邻的 所有节点
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
using namespace std;
int a[],n,b[],visited[],q,m,mark; // 一共有几个数字 ? 数字储存
void DFS(int s,int sum) // 先 弄第一个
{
if(sum>m) // 这种 剪枝 能有一个就有一个
return ;
if(sum==m) // 输出
{
printf("%d",b[]);
for(int i=;i<q;i++)
printf("+%d",b[i]);
printf("\n");
mark=;
return ;
}
for(int i=;i<=n;i++)
{
if(visited[i]) // 发现 已经 用过了 那就 直接下一步 吧 .
continue;
if(a[i]<=m-sum&&b[q-]>=a[i]) // 能放进去 并且 是递减顺序 // 根据题目中的 要求 来限定条件 搜索 也算是一种 模拟呀 .
{
visited[i]=;
sum+=a[i];
b[q++]=a[i];
DFS(s,sum);
q--;
sum-=a[i];
visited[i]=;
while(a[i]==a[i+]) /* 如果有两个相同的数字在一起 当以其中某一个数字为开头的话 从后面找到一个符合题意的解 轮到和这个数字相同的的数值的时候 会又出现以i从这样的情况所以需要直接这样跳过去*/
i++; // 不过 如果跳的时候 需要实现 有序排列
}
} }
int main()
{
while(scanf("%d%d",&m,&n),(n||m))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
printf("Sums of %d:\n",m);
memset(visited,,sizeof(visited));
mark=q=;
b[]=;
DFS(,);
if(mark)
printf("NONE\n");
}
}
人逢A题 , 精神爽 , 来一发改进版的 , 这个答案是做了http://www.cnblogs.com/A-FM/p/5334822.html Stick神题得到的启示
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
using namespace std;
int n,m,a[],q,b[],visited[],flag;
void DFS(int sum,int mark,int index)
{
if(sum==m)
{
printf("%d",b[]);
for(int i=;i<mark;i++)
printf("+%d",b[i]);
printf("\n");
flag=;
}
if(sum>m)
return ;
for(int i=index;i<n;i++)
{
if(visited[i]||(a[i]==a[i-]&&!visited[i-])) // 这一个和上一个相同而且 上一个 没用 这个就也不用了
continue;
if(sum+a[i]<=m)
{
visited[i]=;
b[mark]=a[i];
DFS(sum+a[i],mark+,i+);
visited[i]=;
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)&&(n||m))
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
memset(visited,,sizeof(visited));
printf("Sums of %d:\n",m);
flag=;
DFS(,,);
if(flag)
printf("NONE\n");
}
return ;
}
Sum It Up -- 深搜 ---较难的更多相关文章
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- 简单深搜:POJ1546——Sum it up
结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 ...
- (深搜)Sum It Up -- poj --1564
链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- USACO 2.3.3 Zero Sum 和为零(深搜枚举)
Description 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入“+”表示加,或者“-”表示减,抑或是“ ”表示空白,来将 ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
随机推荐
- python之cookbook-day02
第一章:数据结构和算法 1.2 解压可迭代对象赋值给多个变量 问题: 如果一个可迭代对象的元素个数超过变量个数时,会抛出一个 ValueError .那么 怎样才能从这个可迭代对象中解压出 N 个元素 ...
- Reading Lists
* Non-academic 1. Slowing Down to the Speed of Life, by Richard Carlson and Joseph Bailey.2. Your Mo ...
- BZOJ 1782 洛谷 2982 [Usaco2010 Feb]slowdown 慢慢游
[题解] 一头牛走到i,相当于把i点的子树的点权都加1,查询减慢的次数就是查询目的地的点权. 预处理dfs序,某个点的子树的dfs序是连续的一段.差分后用树状数组维护,变成点修区查.或者直接线段树区修 ...
- codeforces 372 Complete the Word(双指针)
codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...
- Spring核心技术(四)——Spring的依赖及其注入(续二)
前面两篇文章描述了IoC容器中依赖的概念,包括依赖注入以及注入细节配置.本文将继续描述玩全部的依赖信息. 使用 depends-on 如果一个Bean是另一个Bean的依赖的话,通常来说这个Bean也 ...
- redis学习——数据类型
一.内容简介 Redis不仅仅是简单的key-value 存储器,同时也是一种data structures server.传统的key-value是指支持使用一个key字符串来索引value字符串的 ...
- [置顶]
Java Web学习总结(25)——MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
这次换了台电脑,所以需要重新配置一次项目开发环境,过程中的种种,记录下来,便于以后再次安装,同时给大家一个参考. 1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64 ...
- better-scroll & scroll
scroll better-scroll https://github.com/ustbhuangyi/better-scroll/blob/master/README.md#getting-star ...
- HBase的集群搭建
前提:已经安装过jdk,HDFS集群和zookeeper,我的集群规划见HDFS的文章中 1.在1上安装配置hbase 下载:http://mirror.bit.edu.cn/apache/hbase ...
- Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...