Exchange Cards(dfs)
Exchange Cards
Time Limit: 2 Seconds Memory Limit: 65536 KB
As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his friends for cards he likes. Of course, different cards have different value, and Mike must use cards he owns to get the new one. For example, to get a card of value 10$, he can use two 5$ cards or three 3$ cards plus one 1$ card, depending on the kinds of cards he have and the number of each kind of card. And Sometimes he will involve unfortunately in a bad condition that he has not got the exact value of the card he is looking for (fans always exchange cards for equivalent value).
Here comes the problem, given the card value he plans to get and the cards he has, Mike wants to fix how many ways he can get it. So it's you task to write a program to figure it out.
Input
The problem consists of multiple test cases, terminated by EOF. There's a blank line between two inputs.
The first line of each test case gives n, the value of the card Mike plans to get and m, the number of different kinds of cards Mike has. n will be an integer number between 1 and 1000. m will be an integer number between 1 and 10.
The next m lines give the information of different kinds of cards Mike have. Each line contains two integers, val and num, representing the value of this kind of card, and the number of this kind of card Mike have.
Note: different kinds of cards will have different value, each val and num will be an integer greater than zero.
Output
For each test case, output in one line the number of different ways Mike could exchange for the card he wants. You can be sure that the output will fall into an integer value.
Output a blank line between two test cases.
Sample Input
5 2
2 1
3 1 10 5
10 2
7 2
5 3
2 2
1 5
Sample Output
1 7
题解:求种类数,i*card.v代表可以加上的价值;
代码:
#include<stdio.h>
struct Node {
int v,num;
};
Node card[];
int N,M,tot;
void dfs(int x,int now){
if(now==N){
tot++;return;
}if(now>N||x>=M)return;
for(int i=;i<=card[x].num;i++){
dfs(x+,now+i*card[x].v);
}
}
int main(){int flot=;
while(~scanf("%d%d",&N,&M)){
if(flot++)puts("");
for(int i=;i<M;i++)scanf("%d%d",&card[i].v,&card[i].num);
tot=;
dfs(,);
printf("%d\n",tot);
}
return ;
}
Exchange Cards(dfs)的更多相关文章
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 搜索——深度优先搜索(DFS)
设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...
随机推荐
- 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)
Hypersphere Time Limit: 1 Second Memory Limit: 32768 KB In the world of k-dimension, there's a ...
- Unity 移动MM自签名方式
在使用Unity接移动MM SDK的时候,最后有一个签名. 主要是把计费文件和版权文件放入APK的根目录. 搞了半天才知道前来这么简单..... 软件使用: apk签名工具apktool
- IOS 网络判断
Reachability *connectionNetWork= [Reachability reachabilityForInternetConnection] ; int status = [co ...
- SQL With(递归 CTE 查询)
本文来自:http://www.cnblogs.com/smailxiaobai/archive/2012/01/16/2323291.html 指定临时命名的结果集,这些结果集称为公用表表达式 (C ...
- Linux FTP安装与简单配置
1.检測是否原有启动 ps -ef|grep vsftpd 2.检測是否有安装包 rpm -qa|grep vsftpd 3.假设有输出.查看状态并启动 service vsftp status - ...
- PC和ARM平台编译Qt的命令
编译for PC 的Qt过程是: (1)qmake -project (qmake命令,用于创建hello.pro,将所有的文件编译成一个与平台无关的工程文件).(注意:按照前面步骤安装好Qt环境之后 ...
- (转)MVC语法-@helpers和@functions(Razor内定义函数)
(转)MVC语法-@helpers和@functions(Razor内定义函数) 转自:http://www.mikesdotnetting.com/Article/173/The-Differenc ...
- [Math]Reverse Integer
Total Accepted: 111287 Total Submissions: 474471 Difficulty: Easy Reverse digits of an integer. Exam ...
- SharePoint 2013 实战碎嘴(ECMAScript客户端对象模型): 提示某个列表不存在
简单情景描述1:(在Sharepoint 2013 Solution 中) 在相应的.aspx页面引入 一下两个.js文件: <script type="text/javascript ...
- 如何用EXCEL表计算今天是本年的第几周?
单元格内输入如下代码计算出来的数字即是当日在本年度的第几周,如下: =INT((TODAY()-DATE(YEAR(TODAY()),1,1)-WEEKDAY(DATE(YEAR(TODAY()),1 ...