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的值等于纸条上的值,则不能切。

2、如果没有办法把纸条上的数字切成不超过target,则输出error。

如target是1而纸条上的数字是123,则无论你如何切得到的和都比1大。

3、如果有一种以上的切法得到最佳值,则输出rejected。

如target为15,纸条上的数字是111,则有以下两种切法11、1或者1、11.

如果有且仅有一种最佳方案,则输出最大值和切割的方案

思路:用dfs搜索每一种可能的情况,取不超过target的最大值,并记录分割的方案

#include<stdio.h>
#include<string.h>
int ans,m,n,vis[1000000],step[10],now[10],num;
char s[10];
void dfs(int pos,int sum,int cnt)
{
    int i,t;
    if(pos>=m){
        vis[sum]++;
        if(sum>ans){
            ans=sum;  //记录最佳切割的和
            num=cnt;
            for(i=0;i<cnt;i++) //记录当前最佳方案
                step[i]=now[i];
        }
        return ;
    }
    t=0;
    for(i=pos;i<m;i++){
        t=t*10+s[i]-'0';
        if(sum+t>n)
            return ;
        now[cnt]=t;
        dfs(i+1,sum+t,cnt+1);
    }
}
int main()
{
    int i,sum;
    while(scanf("%d%s",&n,s)!=EOF){
        m=strlen(s);
        if(n==0&&s[0]=='0'&&m==1)
            break;
        sum=0;
        for(i=0;i<m;i++)
            sum+=s[i]-'0';
        if(sum>n){   //若最小的和都大于targe,则肯定不可能切割出不超过targe的和
            printf("error\n");
            continue;
        }
        memset(vis,0,sizeof(vis));
        num=ans=0;
        dfs(0,0,0);
        if(vis[ans]>1)
            printf("rejected\n");
        else{
            printf("%d",ans);
            for(i=0;i<num;i++)
                printf(" %d",step[i]);
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-10-08 20:50:28

poj 1416 Shredding Company (dfs)的相关文章

搜索+剪枝 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

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

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

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

【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