Educational Codeforces Round 108 (Rated for Div. 2) B. The Cake Is a Lie(简单DP)
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≤𝑡≤1001≤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≤10^4) — 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
Copy
6
1 1 0
2 2 2
2 2 3
2 2 4
1 4 3
100 100 10000
output
Copy
YES
NO
YES
NO
YES
NO
简单dp, dp[i, j, k]表示从(1, 1)能否花费k走到(i, j),转移方程不难推出(见代码
注意数组开int会mle。
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool dp[101][101][10001];//不用开int
int main() {
int t;
cin >> t;
memset(dp, 0, sizeof(dp));
dp[1][1][0] = 1;
for(int i = 1; i <= 100; i++) {
for(int j = 1; j <= 100; j++) {
for(int w = 1; w <= 10000; w++) {
if(w - j >= 0) dp[i][j][w] |= dp[i - 1][j][w - j];
if(w - i >= 0) dp[i][j][w] |= dp[i][j - 1][w - i];
}
}
}
while(t--) {
cin >> n >> m >> k;
if(dp[n][m][k]) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}