hdu 1258 DFS
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
题意比较好懂,解析见代码
代码:
/*
hdu1258
dfs,小数据,dfs暴力搜一遍即可,之前一直做一些图的题目,这算是做的
第一道比较抽象的dfs题目,dfs最重要的思想是递归与回溯来实现状态的转移,是
一种暴力的搜索手段,适用于小数据的情况
*/
#include<iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=15;
const double epi=1e-8;
const double pi=acos(-1.0);
int a[maxn],b[maxn];
bool v[maxn];//标记数组,避免在一次搜索中重复搜索
int tar,n;
bool flag;
void dfs(int sum,int pos,int ans)//三个参数,sum代表当前层计数总和,判断递归是否结束的标志,pos储存下一次从哪一个位置开始搜索
{
int i;
if(sum>tar) return;
if(sum==tar)
{
flag=true;
for(int i=1;i<=ans;i++)
printf((i==ans)?"%d\n":"%d+",b[i]);//输出注意格式
}
int last=-1;
for(i=pos+1;i<=n;i++)
{
if(!v[i]&&a[i]!=last)
{
b[ans+1]=a[i];
last=a[i];
v[i]=true;
dfs(sum+a[i],i,ans+1);
v[i]=false;
}
}
}
int main()
{
while(scanf("%d%d",&tar,&n)&&(tar||n))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(v,false,sizeof(v));
cout<<"Sums of "<<tar<<":"<<endl;
flag=false;//判断是否找到答案
dfs(0,0,0);
if(!flag)
cout<<"NONE"<<endl;
}
}
hdu 1258 DFS的更多相关文章
- poj1564 Sum It Up (zoj 1711 hdu 1258) DFS
POJhttp://poj.org/problem?id=1564 ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=711 ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- HDU 1258 Sum It Up(dfs 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 2266 dfs+1258
How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- (step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...
随机推荐
- HTML 学习网站
http://www.w3school.com.cn/tiy/t.asp?f=html_intro
- C++ 基本数据结构整理
Hash Map (Unordered_map) Insert #include <unordered_map> using namespace std; unordered_map &l ...
- phpmyadmin自增字段
自增字段必须为primary key 2种方法: 1- ALTER TABLE `qr_role` CHANGE `ROLE_ID` `ROLE_ID` INT(11) NOT NULL AUTO_I ...
- mysql for python,银行转账模拟
学习中, 本人为初学者.勿喷. #-*- coding:utf-8 -*- import MySQLdb class Tranferaccount(object): def __init__(self ...
- Pyzo -- 好用的 Python 轻量级 IDE
近期 yvivid 使用 Python 进行科学计算类应用(如matlab部分应用场景) 比较好的 发行版本为 Anaconda: A free distribution for the SciPy ...
- Java学习笔记--String StringBuffer StringBuilder
String StringBuffer StringBuilder String http://docs.oracle.com/javase/7/docs/api/ 中文: http://www.cn ...
- shell编程技术之-基础知识
一.脚本结构 linux下shell的脚本,是将一系列命令序列写在一个文本文件,而这个文本文件时可执行的.相对命令行来说,开发效率提高.因此他的构架有2部分构成#!和命令序列.其中#!指明此脚本是用哪 ...
- MySQL 优化方案
基本上通过索引来解决 . 通常索引键在where , group by , order by 相关的列 一个表只能用一个索引(查询的时候)所以当要执行复杂查询时最好使用联合索引就是 index (a, ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 深入浅出Node.js (8) - 构建Web应用
8.1 基础功能 8.1.1 请求方法 8.1.2 路径解析 8.1.3 查询字符串 8.1.4 Cookie 8.1.5 Session 8.1.6 缓存 8.1.7 Basic认证 8.2 数据上 ...