NYoj The partial sum problem(简单深搜+优化)

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <algorithm>
 6 #include <iostream>
 7 using namespace std;
 8
 9 int n,k;
10 int a[21];
11 int sum=0;
12 bool v[21];
13
14
15 int DFS(int sum,int m)
16 {
17     if(sum==k)
18     {
19         return 1;
20     }
21     for(int j=m;j<n;j++)
22     {
23         v[j]=true;
24         if(DFS(sum+a[j],j+1)) return 1;//要深刻理解这个递归
25         v[j]=false;
26     }
27     return 0;
28 }
29
30 int main()
31 {
32     while(~scanf("%d",&n)){
33         for(int i=0;i<n;i++){
34             scanf("%d",&a[i]);
35         }
36         scanf("%d",&k);
37         if(DFS(0,0))
38             printf("Of course,I can!\n");
39         else
40             printf("Sorry,I can‘t!\n");
41     }
42 }
时间: 2024-12-26 20:03:38

NYoj The partial sum problem(简单深搜+优化)的相关文章

NYOJ 927 The partial sum problem 【DFS】+【剪枝】

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 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 mul

hdu1016(简单深搜)

这是一个简单深搜问题,要求相邻的数之间相加为素数.采用深搜,把满足条件的值放在parent[]中.最后输出parent[]. A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: th

NYOJ 迷宫寻宝(一)深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 做这道题的时候迷迷糊糊的,,果然比较难..最后也是没有做出来..请教了一下学长,学长说我基础还不好..基础果然重要,这道题是一道搜索题,我没有考虑钥匙在门后面的情况,比如aBbSAG 多亏学长指教,通过这道题对深搜的理解又加深了一步~ #include <iostream> #include <cstdio> #include <cstring> using

fzu 1920 Left Mouse Button(简单深搜题)

题目地址:http://acm.fzu.edu.cn/problem.php?pid=1920 题目大意是给定一个n*n的图,模拟扫雷游戏,0代表没有雷区,1代表附近九宫格内只有一个雷-- 如果不懂的话去玩下扫雷,挺好玩的,99个雷的玩了好几百盘才赢了一次............ 此题假设神操作,点不到雷,问你最少要多少下才可以把图中非雷的点完,怎么样才最快呢? 当然先点0啦,,,,,,,, 好吧,不废话了..... ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1

nyoj927 The partial sum problem(dfs)

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 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 multi

ZOJ Seeding 2100【简单深搜】

Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares. Tom has a seeding-ma

NYOJ-927 The partial sum problem

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 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 multi

简单深搜:POJ1546——Sum it up

结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 可以尝试去画一下二叉树理解一下,查看遍历的路径 代码还是百度到别人的自己再参悟- -佩服别人的功底啊 先上代码: /*POJ 1546 Sum it up*/ # include<iostream> # include<algorithm> # include<cstdio&g

HDU 1258 Sum It Up 深搜

 Crawling in process... Crawling failed   Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1258 Description Given a specified total t and a list of n integers, find all distinct sums using numb