POJ 1416 DFS
题目翻译:
公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值。比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1、2、34、6。因为这样所得到的和43 (= 1 + 2 + 34 + 6) 是所有可能中最接近而不超过50的。(比如1, 23, 4, 和6 就不可以,因为它们的和不如43接近50,而12, 34, 6也不可以,因为它们的和超过50了。碎纸还有以下三个要求:
1、如果target的值等于纸条上的值,则不能切。
2、如果没有办法把纸条上的数字切成小于target,则输出error。如target是1而纸条上的数字是123,则无论你如何切得到的和都比1大。
3、如果有超过一种以上的切法得到最佳值,则输出rejected。如target为15,纸条上的数字是111,则有以下两种切法11、1或者1、11.
你的任务是编写程序对数字进行划分以达到最佳值。
题目翻译转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304031265
思路:
一发暴搜敲上去,就A了。。没发现什么坑
// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,minn,sum,len,ans[70],vis[70],flag;
char m[70];
void dfs(int pos,int temp){
if(temp>n)return;
if(pos==len+1&&minn<=temp){
if(minn==temp)flag=1;
else{
for(int i=1;i<=len;i++)ans[i]=vis[i];
minn=temp;flag=0;
}
}
int jy=0;
for(int i=pos;i<=len;i++){
jy=m[i]+jy*10;
vis[i]=1;
dfs(i+1,temp+jy);
vis[i]=0;
}
}
int main(){
while(scanf("%d%s",&n,m+1)&&n){
len=strlen(m+1);flag=minn=sum=0;
for(int i=1;i<=len;i++)m[i]-='0',sum+=m[i];
if(sum>n){puts("error");continue;}
dfs(1,0);
if(flag){puts("rejected");continue;}
printf("%d ",minn);
for(int i=1;i<=len;i++){
printf("%d",m[i]);
if(ans[i])putchar(' ');
}
putchar('\n');
}
}
POJ 1416 DFS的更多相关文章
- POJ 1416 Shredding Company【dfs入门】
题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Tot ...
- 搜索+剪枝 POJ 1416 Shredding Company
POJ 1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5231 Accep ...
- poj 1416 Shredding Company( dfs )
我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...
- POJ 1416 Shredding Company 回溯搜索 DFS
Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6173 Accepted: 3361 ...
- OpenJudge 2803 碎纸机 / Poj 1416 Shredding Company
1.链接地址: http://poj.org/problem?id=1416 http://bailian.openjudge.cn/practice/2803 2.题目: 总时间限制: 1000ms ...
- POJ 1416 Shredding Company
题目: http://poj.org/problem?id=1416 又16ms 1A了,这人品... #include <stdio.h> #include <string.h&g ...
- POJ 1321 DFS
题意:POJ少见的中文题,福利啊. 思路: 一开始也没有思路呃呃呃 . 裸搜,连样例都过不去...参照了网上的题解:一行一行DFS 茅塞顿开啊. #include <cstdio> #in ...
- POJ 1979 dfs和bfs两种解法
fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...
- poj 1190 DFS 不等式放缩进行剪枝
F - (例题)不等式放缩 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
随机推荐
- Python之first script
1 A first script 1) script1.py - imports a Python module (libraries of additional tools) to fetch th ...
- WebGL绘制三角形
本文程序实现绘制一个三角形的任务,如下图. 整个程序包含两个文件,分别是: 1. HelloTriangle.html <!DOCTYPE HTML PUBLIC "-//W3C//D ...
- PHP实现几秒前、几分钟前、几小时前、几天前
/** * @Description: 将时间转换为几秒前.几分钟前.几小时前.几天前 * @Author: Yang * @param $the_time 需要转换的时间 * @return str ...
- Unity与Android通信的中间件
2.1.1 Fragment和Activity都需要实现的接口——IBaseView/** * Description:Basic interface of all {@link Activity} ...
- PAT_A1119 Pre- and Post-order Traversals
Source: PAT A1119 Pre- and Post-order Traversals (30 分) Description: Suppose that all the keys in a ...
- Keras手写识别例子(1)----softmax
转自:https://morvanzhou.github.io/tutorials/machine-learning/keras/2-2-classifier/#测试模型 下载数据: # downlo ...
- 洛谷P1583 魔法照片【模拟+排序】
一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从大到小进行排序,每人就有 ...
- Linux shell文本处理工具
搞定Linux Shell文本处理工具,看完这篇集锦就够了 Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕竟学习She ...
- 洛谷 P3275 BZOJ 2330 [SCOI2011]糖果
题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...
- Hive教程(1)
1. 介绍 Apache Hive可以使用SQL来读,写,管理分布式存储的大数据集,结构可以投射到已经存储的数据上,命令行工具和JDBC驱动可以让用户连接到Hive. 2. 安装和配置 你可以下载Hi ...