codeforces B. The Cake Is a Lie(动态规划)

该博客讨论了一种使用动态规划解决代码forces问题B.TheCakeIsaLie的方法。题目要求从(1,1)到达(n,m)花费恰好k burles。通过分析状态转移方程,博主展示了如何从起点开始递推,最终判断是否能到达目标位置并精确花费k。代码实现采用C++,并给出了样例输入和输出。

题目描述

题目链接:codeforces B. The Cake Is a Lie
There is a n×m grid. You are standing at cell (1,1) and your goal is to finish at cell (n,m).

You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell (x,y). You can:

move right to the cell (x,y+1) — it costs x burles;
move down to the cell (x+1,y) — it costs y burles.
Can you reach cell (n,m) spending exactly k burles?

Input
The first line contains the single integer t (1≤t≤100) — the number of test cases.

The first and only line of each test case contains three integers n, m, and k (1≤n,m≤100; 0≤k≤104) — the sizes of grid and the exact amount of money you need to spend.

Output
For each test case, if you can reach cell (n,m) spending exactly k burles, print YES. Otherwise, print NO.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

Example
input

6
1 1 0
2 2 2
2 2 3
2 2 4
1 4 3
100 100 10000

output

YES
NO
YES
NO
YES
NO

Note
In the first test case, you are already in the final cell, so you spend 0 burles.

In the second, third and fourth test cases, there are two paths from (1,1) to (2,2): (1,1) → (1,2) → (2,2) or (1,1) → (2,1) → (2,2). Both costs 1+2=3 burles, so it’s the only amount of money you can spend.

In the fifth test case, there is the only way from (1,1) to (1,4) and it costs 1+1+1=3 burles.

分析

1.定义dp[x][y][k]为处于(x,y)处时是否正好花费为k(把dp数组定义为布尔类型,为true时表示正好为k,为false表示不可能正好为k)。
2.确定状态转移方程:
(1)正向思考:哪些状态和dp[x][y][k]有关或dp[x][y][k]能推出哪些状态?
     若(x,y)处正好花费k,则向下走到达(x+1,y),花费为y,可以推知的状态为dp[x+1][y][k+y]。向右走到达(x,y+1),花费为x,可以推知的状态为dp[x][y+1][k+x]。

(2)逆向思考确定状态转移方程:dp[x][y][k]由哪些状态确定?
     由于只能向下或向右走,故(x,y)的上一个位置只能是(x-1,y)或(x,y-1),状态“处于(x,y)处且恰好花费为k”的状态由dp[x-1][y][k-y]和dp[x][y-1][k-x]确定。故dp[x][y][k]=dp[x-1][y][k-y] || dp[x][y-1][k-x]。(前两个状态中有一个为true,dp[x][y][k]就为true)。还要注意一点,递推的时候要保证数组不越界,故要保证k-y>=0,k-x>=0。所以最终的状态转移方程为:
d p [ x ] [ y ] [ k ] = { d p [ x ] [ y − 1 ] [ k − x ] ∣ ∣ d p [ x − 1 ] [ y ] [ k − y ] , ( k − x ≥ 0 , k − y ≥ 0 ) d p [ x ] [ y − 1 ] [ k − x ] , ( k − x ≥ 0 , k − y < 0 ) d p [ x − 1 ] [ y ] [ k − y ] , ( k − x < 0 , k − y ≥ 0 ) 0 , ( k − x < 0 , k − y < 0 ) dp[x][y][k]=\left\{\begin{matrix} dp[x][y-1][k-x] || dp[x-1][y][k-y],(k-x\geq 0,k-y\geq 0 ) \\ dp[x][y-1][k-x],(k-x\geq 0,k-y< 0) \\dp[x-1][y][k-y],(k-x<0,k-y\geq 0) \\0,(k-x<0,k-y<0) \end{matrix}\right. dp[x][y][k]=dp[x][y1][kx]dp[x1][y][ky],(kx0,ky0)dp[x][y1][kx],(kx0,ky<0)dp[x1][y][ky],(kx<0,ky0)0,(kx<0,ky<0)

3.边界处理:处于起点(1,1)时花费恰好为0,故设dp[1][1][0]=true。其它状态初始化为false。

4.t组数据,每组数据都是“询问位置(n,m)处是否恰好花费为k”,故在这些询问中取一个最大"n,m,kkk",递推出这个最大的范围的情况,其它小的范围的询问也可以在最后被查到。

C++代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e2+10,M=1e2+10,K=1e4+10;
bool dp[N][M][K];
int nn[N],mm[M],kk[K];


int main(){

int t,n=-1,m=-1,kkk=-1;
scanf("%d",&t);

//读入t组询问,同时在其中找一个“范围最大”的询问“n,m,kkk”
for(int i=1;i<=t;i++) {
    scanf("%d%d%d",&nn[i],&mm[i],&kk[i]);
    n=max(n,nn[i]);
    m=max(m,mm[i]);
    kkk=max(kkk,kk[i]);
}

 memset(dp,false,sizeof(dp));
 dp[1][1][0]=true;   //边界处理
 
 //递推
for(int x=1;x<=n;x++){
    for(int y=1;y<=m;y++){
            int s=min(x,y);
        for(int k=s;k<=kkk;k++){
            if(k>=x) {  // k-x >=0
                dp[x][y][k]=dp[x][y-1][k-x];
                if(k>=y){   //k-x>=0 并且 k-y>=0
                    dp[x][y][k]|=dp[x-1][y][k-y];
                }
            }
           else{
            //k-x<0 且k-y>=0
            if(k>=y)     dp[x][y][k]=dp[x-1][y][k-y];
           }

      }
    }
   }

 //输出t组询问的答案
for(int i=1;i<=t;i++){
    if(dp[nn[i]][mm[i]][kk[i]]) printf("YES\n");
    else printf("NO\n");
}

return 0;
}
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值