zoj 2156 Charlie's Change(多重背包+倍增优化+记录路径)

Charlie's Change

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task.

Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.

Input Specification

Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line  P, 1  <=  P  <= 10 000, is the coffee price in cents. Next four integers,  C 1C 2C 3C 4, 0  <=  C i  <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.

Output Specification

For each situation, your program should output one line containing the string " Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where  T 1T 2T 3T 4 are the numbers of coins of appropriate values Charlie should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output " Charlie cannot buy coffee.".

Sample Input

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.


Source: Czech Technical University Open 2003

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2156

分析:这题还是付钱的问题,做法依旧是多重背包,比较不一样的是这题要求出背包的具体装法,也就是记录一下转移路径就行了

代码:

#include<cstdio>
using namespace std;
const int mm=11111;
int v[]= {1,5,10,25};
int f[mm],p[mm],t[mm],c[4],ans[4];
int i,j,m;
void CompletePack(int v,int k)
{
    for(int i=v;i<=m;++i)
        if(f[i-v]>=0&&f[i]<=f[i-v])
        {
            f[i]=f[i-v]+1;
            p[i]=i-v;
            t[i]=k;
        }
}
void ZeroOnePack(int v,int d,int k)
{
    for(int i=m;i>=v;--i)
        if(f[i-v]>=0&&f[i]<f[i-v]+d)
        {
            f[i]=f[i-v]+d;
            p[i]=i-v;
            t[i]=k;
        }
}
int main()
{
    while(scanf("%d%d%d%d%d",&m,&c[0],&c[1],&c[2],&c[3]),m+c[0]+c[1]+c[2]+c[3])
    {
        for(i=0; i<=m; ++i)f[i]=-1000000000;
        f[0]=0;
        for(i=0; i<4; ++i)
            if(c[i])
            {
                if(c[i]*v[i]>=m)CompletePack(v[i],i);
                else
                {
                    j=1;
                    while(j<c[i])
                    {
                        ZeroOnePack(v[i]*j,j,i);
                        c[i]-=j;
                        j<<=1;
                    }
                    ZeroOnePack(v[i]*c[i],c[i],i);
                }
            }
        if(f[m]>=0)
        {
            for(i=0; i<4; ++i)ans[i]=0;
            while(m)
            {
                ans[t[m]]+=(m-p[m])/v[t[m]];
                m=p[m];
            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[0],ans[1],ans[2],ans[3]);
        }
        else puts("Charlie cannot buy coffee.");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题是一个典型的搜索问题,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来解决。以下是使用DFS的代码实现: ```c++ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 20; const int MAXM = 20; int n, m, sx, sy, ex, ey; char maze[MAXN][MAXM]; // 迷宫 int vis[MAXN][MAXM]; // 标记数组 int dx[] = {0, 0, 1, -1}; // 方向数组 int dy[] = {1, -1, 0, 0}; void dfs(int x, int y) { if (x == ex && y == ey) { // 到达终点 printf("(%d,%d)", x, y); return; } for (int i = 0; i < 4; i++) { // 依次尝试四个方向 int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && !vis[nx][ny]) { vis[nx][ny] = 1; // 标记已访问 printf("(%d,%d)->", x, y); dfs(nx, ny); return; } } } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'S') { sx = i; sy = j; } else if (maze[i][j] == 'T') { ex = i; ey = j; } } } vis[sx][sy] = 1; dfs(sx, sy); printf("\n"); } return 0; } ``` 代码实现中,使用了一个标记数组 `vis` 来标记每个位置是否已经被访问过,避免走重复的路线。使用DFS的时候,每次从当前位置依次尝试四个方向,如果某个方向可以走,则标记该位置已经被访问过,并输出当前位置的坐标,然后递归进入下一个位置。如果当前位置是终点,则直接输出并返回。 在输出路径的时候,由于是递归调用,所以输出的路径是反向的,需要将其反转过来,即从终点往起点遍历输出。 需要注意的是,题目中要求输出的路径是 `(x1,y1)->(x2,y2)->...->(xn,yn)` 的形式,每个坐标之间用 `->` 连接。所以在输出的时候需要特别处理第一个坐标和最后一个坐标的格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值