POJ之1014
来源:互联网 发布:手机打不开数据流量 编辑:程序博客网 时间:2024/05/22 07:53
/*
两人分石头,要求平分,但石头大小不一,价值也不一样,从1到20000,如果一堆石头的总价是奇数显然不可分,则输出不可分割
否则再看这堆石头是否能被分成两份,在保证石头完好无损的情况下。
输入1 0 1 2 0 0代表价值为1的1个,价值为2的0个,·····;即有四块价值分别为4,4,3,1的石头,显然这石头是没法分的
输入1 0 0 0 1 1代表·····,即有三块价值为6,5,1的石头,显然可分,一份为6,另一份为5,1;
有两种解决方法:
第一种是同学都熟悉的多重背包问题
第二种方法是很少有同学使用的DFS*/
#include <iostream>#include<stdio.h>#include<string>#include<algorithm>#include<vector>#include<queue>#include<set>#include<map>#include<math.h>#include<limits.h>#include<stdlib.h>using namespace std;bool divided=false;int marble[7]= {0};void DFS(int value,int target,int pre){ if(divided) return; if(value==target) { divided=true; return; } for(int i=pre; i>=1; i--) { if(marble[i]) { if(value+i<=target) { marble[i]--; DFS(value+i,target,i); if(divided) break; } } } return;}int main(){ int pre = 6; int evenValue_of_marble;//石块的均值 int flag = 1; while(true) { int i=1; int value_of_marble = 0;//所有石块的价值 int number_of_marble = 0;//石块的个数 while(i <= pre) { cin >> marble[i]; value_of_marble += marble[i] * i; number_of_marble += marble[i]; i++; } if(number_of_marble > 20000) { break; } if(number_of_marble > 0) { if(value_of_marble % 2 == 0) { evenValue_of_marble = value_of_marble / 2; DFS(0,evenValue_of_marble,pre); if(divided) { cout<<"Collection #"<<flag<<":"<<endl; cout<<"Can be divided."<<endl<<endl; flag++; continue; } else { cout<<"Collection #"<<flag<<":"<<endl; cout<<"Can't be divided."<<endl<<endl; flag++; continue; } } else { cout<<"Collection "<<"#"<<flag<<":"<<endl; cout<<"Can't be divided."<<endl<<endl; flag++; continue; } } else { break; } } return 0;}
0 0
- POJ之1014
- poj之动态规划
- poj 1002之我见
- POJ之路
- DP 之 poj 2229
- DP 之 poj 2385
- DP 之 poj 3616
- DP 之 poj 3280
- DP 之 poj 2955
- poj之3252
- C++之POJ-6
- POJ之1007
- POJ之1012
- poj之旅_2184
- POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】
- poj 2754/1014 多重背包之二进制优化
- POJ 1014
- poj 1014
- golang学习之--简单的web网站
- apache/lighttpd/nginx 对比
- MySQL 会话变量 和 系统变量
- RocketMQ与Kafka对比(18项差异)评价版
- android 4种定位原理及实现——1
- POJ之1014
- spring 安装和使用
- python twisted聊天服务器
- 多线程教程之二---线程间同步
- sysfs文件系统
- http://steamcommunity.com/profiles/76561198184458547/
- Swift语法基础:7 - Swift的Generics
- old_blog 皇后
- 单片机数字滤波的算法(