zoj 2734 Exchange Cards[DFS or 모함수]
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
: N, M , , N。
:0ms
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct rec
{
int val, num;
};
rec TT[20];
int N, M;
int c1[10100], c2[10100];
int main()
{
int t = 0;
while(scanf("%d%d", &N, &M) != EOF)
{
if(t > 0) printf("
");
for(int i = 1; i <= M; i++)
scanf("%d%d", &TT[i].val, &TT[i].num);
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
for(int i = 0, used = 0; used <= TT[1].num; i+=TT[1].val, used++)
c1[i] = 1;
for(int i = 2; i <= M; i++)
{
for(int j = 0; j <= N; j++)
{
for(int k = 0, used = 0; k + j <= N && used <= TT[i].num; k+=TT[i].val, used++)
c2[k+j] += c1[j];
}
for(int j = 0; j <= N; j++)
{
c1[j] = c2[j];
c2[j] = 0;
}
}
printf("%d
", c1[N]);
t++;
}
return 0;
}
DFS:0ms 분할 아이템 + 가지치기#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int rec[10000010];
int ans;
int N, M;
int k;
void DFS(int len, int pos)
{
if(len == N)
{
ans++;
return ;
}
for(int i = pos; i < k; i++)
{
if(len + rec[i] <= N)
DFS(len + rec[i], i+1);
while(rec[i] == rec[i+1] && i < k)//
++i;
}
}
int main()
{
int t = 0;
while(scanf("%d%d", &N, &M) != EOF)
{
if(t > 0) printf("
");
int x, y;
k = 0;
for(int i = 1; i <= M; i++)
{
scanf("%d%d", &x, &y);
while(y--)
rec[k++] = x;
}
ans = 0;
DFS(0, 0);
printf("%d
", ans);
t++;
}
return 0;
}
두 번째 묘사는 다른 사람의 것을 보면 매우 대단하다.orz... #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct rec
{
int val, num;
};
rec TT[20];
int ans;
int N, M;
void DFS(int len, int pos)
{
if(len == N)
{
ans++;
return ;
}
if(pos > M || len > N)
return ;
for(int i = 0; i <= TT[pos].num; i++)
DFS(len + TT[pos].val * i, pos+1);
}
int main()
{
while(scanf("%d%d", &N, &M) != EOF)
{
for(int i = 1; i <= M; i++)
scanf("%d%d", &TT[i].val, &TT[i].num);
ans = 0;
DFS(0, 1);
printf("%d
", ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.