STAMPS POJ 1010
来源:互联网 发布:vb计算圆的面积和周长 编辑:程序博客网 时间:2024/05/21 14:45
dfs 注意题目中的要求。满足顾客需求的解就是可行解。邮票种类最多的可行解为最优。如果存在两个以上的最优解的邮票种类是一样的,张数最少的更优张数也一样的话,这些最优解中最大面值较大的更优。若邮票种类、张数、最大面值三者都分别相同,则认为这些最优解相同,输出tie。没有解就是none。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1000],type[1000],book[1000];int customer,flag=0,flag_tie=0;int solve[6];int ans[6];int max1(int *t){int maxn=-1;for(int i=2;i<6;i++){if(maxn<t[i]){maxn=t[i];}}return maxn;}void update(int all_num,int type_num){ans[0]=type_num;ans[1]=all_num;for(int i=2;i<all_num+2;i++){ans[i]=solve[i-2];}}int dfs(int cos,int all_num,int type_num,int pre){//当前邮票价值 邮票总数 邮票种类总数 前一个邮票编号(用于剪枝) //cout<<cos<<' '<<all_num<<' '<<type_num<<endl;if(all_num>=5) return 0;//最多有四张邮票 if(customer==cos){if(!flag){if(type_num==ans[0]){if(all_num==ans[1]){int maxn=max1(solve);int maxn1=max1(ans);if(maxn1==maxn){//最大面额相等 flag_tie=1;}else if(maxn>maxn1){//选择面额更大的 flag_tie=0;update(all_num,type_num);}}else if(all_num<ans[1]){//选择总数更少的 flag_tie=0;update(all_num,type_num);}}else if(type_num>ans[0]){//选择种类更多的 flag_tie=0;update(all_num,type_num);}}else{flag=1;update(all_num,type_num);}}else if(cos>customer){return 0;}for(int i=pre;a[i]!=0;i++){ if(cos+a[i]<=customer){solve[all_num]=a[i];if(book[i]==0){book[i]++;//cout<<cos<<' '<<a[i]<<endl; dfs(cos+a[i],all_num+1,type_num+1,i); //cout<<a[i+1]<<' '<<cos<<endl; } else{ book[i]++; dfs(cos+a[i],all_num+1,type_num,i); } solve[all_num]=0; book[i]--;}else {//之前排过序,所以若当前的不符合要求则不用继续搜 break;}}}int main(){int i=0;while(true){int i;for(i=0;;i++){if(scanf("%d",&a[i])==EOF){return 0;};if(a[i]==0){break;}}sort(a,a+i);while(scanf("%d",&customer)&&customer){memset(type,0,sizeof(type));memset(book,0,sizeof(book));memset(ans,0,sizeof(ans));memset(solve,0,sizeof(solve));dfs(0,0,0,0);printf("%d",customer); if(ans[1]==0){printf(" ---- none\n"); }else if(flag_tie){printf(" (%d): tie\n",ans[0]);}else{printf(" (%d):",ans[0]);for(int i=2;i<ans[1]+2;i++){printf(" %d",ans[i]);}printf("\n");}}}}
0 0
- POJ 1010 STAMPS
- poj 1010 STAMPS
- poj 1010 STAMPS
- POJ-1010-STAMPS
- [POJ][1010]STAMPS
- POJ 1010 STAMPS
- POJ - 1010 STAMPS
- POJ 1010: STAMPS
- POJ 1010 STAMPS(DFS)
- POJ 1010 STAMPS
- POJ 1010--STAMPS
- STAMPS POJ 1010
- poj 1010:STAMPS
- POJ 1010 STAMPS 笔记
- POJ 1010--STAMPS
- POJ 1010 解题报告 STAMPS
- POJ 1010 STAMPS 解题报告
- UESTC 408 STAMPS // poj 1010
- Android深入浅出自定义控件(二)
- perl 回调函数
- 立即执行函数(IIFE)的理解与运用
- 面向对象测试
- android进程间通信--Binder
- STAMPS POJ 1010
- 【SSH】错误锦集
- Struts2中struts.xml的Action配置详解(一)===》 struts2.0中struts.xml配置文件详解 (二)==》Struts2_struts.xml配置及例程三
- 哥德巴赫猜想(c 循环+素数判断)
- [从头学python] 第07节 numpy的盘点
- 欢迎使用CSDN-markdown编辑器
- 关于Javascript模块化和命名空间管理
- 深度学习进行目标识别的资源列表
- 好工作需要这么多而充足的准备,你还在浪费时间吗 行动吧?