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

题目描述

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值