DFS/POJ 1416 Shredding Company

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int mmax,sum;
 5 char s[10];
 6 bool v[10],way[10];
 7 bool rejected;
 8 void dfs(int dep,int value,int before)
 9 {
10     int now=0;
11     for (int i=before;i<dep;i++)
12     {
13         now=now*10;
14         now=now+s[i]-‘0‘;
15     }
16     if (dep==strlen(s))
17     {
18         int ans=value+now;
19         if (ans>mmax) return;
20         if (ans==sum)
21         {
22             rejected=true;
23             return;
24         }
25         else if (ans>sum)
26         {
27             sum=ans;
28             rejected=false;
29             for (int i=0;i<strlen(s);i++) way[i]=v[i];
30             return;
31         }
32         return;
33     }
34     if (value>mmax) return;
35     v[dep]=1;
36     dfs(dep+1,value+now,dep);
37     v[dep]=0;
38     dfs(dep+1,value,before);
39
40 }
41 int main()
42 {
43     scanf("%d%s",&mmax,s);
44     while (mmax!=0)
45     {
46         memset(v,0,sizeof(v));
47         sum=0;
48         v[0]=1;
49         rejected=false;
50         dfs(1,0,0);
51         if (rejected==true) printf("rejected\n");
52         else if (sum==0) printf("error\n");
53         else
54         {
55             printf("%d",sum);
56             for (int i=0;i<strlen(s);i++)
57             {
58                 if (way[i]==1) printf(" ");
59                 printf("%c",s[i]);
60             }
61             printf("\n");
62         }
63         scanf("%d%s",&mmax,s);
64     }
65     return 0;
66 }
时间: 2024-10-11 17:55:58

DFS/POJ 1416 Shredding Company的相关文章

搜索+剪枝 POJ 1416 Shredding Company

POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accepted: 2964 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would

poj 1416 -- Shredding Company

Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4114   Accepted: 2324 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shre

POJ 1416 Shredding Company【dfs入门】

题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6860   Accepted: 3710 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "

poj 1416 Shredding Company (DFS)

Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4595   Accepted: 2633 Description You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shre

poj 1416 Shredding Company (dfs)

链接:poj 1416 题意:有一种新的碎纸机,要用新的碎纸机将纸条上的数字切成几部分, 求切完后的和最接近而不超过target的值. 比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分, 分别是1.2.34.6.所得到的和43 (= 1 + 2 + 34 + 6) 是不超过50的最大和 比如1, 23, 4, 和6 ,和为34,比43小, 而12, 34, 6不可以,因为它们的和超过50了. 碎纸还有以下三个要求: 1.如果target的值等于纸条上的值,则不能切.

poj 1416 Shredding Company 模拟+枚举

题意: 给一个目标数和一个待分割数,可以对待分割数进行任意划分,比如将带分割数12305分为12,30,5,问将分好的数加起来最接近且不超过目标数的分割方案. 分析: 关键是在对带分割数的任意划分,直接for循环枚举状态,比如状态10101(二进制)表示将12305分为1,23,05. 代码: #include <iostream> #include <vector> using namespace std; int t,len; char a[12]; int vis[10000

【POJ 1416】 Shredding Company

[POJ 1416] Shredding Company dfs貌似不剪枝也能过 数据水水的 不过练练剪枝拓一下思路 每组两个数t num 输入0 0结束 分割数字num为任意组 让这几组加和最接近t(且<=t) 无解输出error 多解输出rejected 否则输出加和还有分割情况 做搜索剪枝有点小经验 搜索的时候逆向搜索 求最大就从大往小搜 求最小就从小往大搜 这样一出现不足(求最大时)或溢出(求最小) 立即return 即可实现高效剪枝 因为此时后继情况均比当前小(大)(为其枝叶) 此题还

Shredding Company (hdu 1539 dfs)

Shredding Company Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 445    Accepted Submission(s): 124 Problem Description You have just been put in charge of developing a new shredder for the Sh

POJ1416——Shredding Company(DFS)

Shredding Company DescriptionYou have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shred sheets of paper into little pieces so that the contents would become unreadable, t