CCPC-Wannafly Winter Camp Day1 (Div2) B pacifier

Category: dp

Portal: Eat Pacman

/*
The first candy appears in t[i][j] seconds
because: the kth second is on the grid of the i-th row and the j-th column, if t[i][j]|k is satisfied, a candy will be obtained
because: The number of our C candies is only required to be 1018 at most, n, m<=10, so the maximum k is 10180,
we can consider using dp[i][j][k] to save it on the i, j grid, in the kth second, the most The number of candies obtained, if k is divisible by t[i][j], our candy number will be increased by 1
because: on the grid of the i-th row and the j-th column, then he can choose to go up, down, left, and right in the next second. Or stop in place
. Therefore, in a certain second, it is also transferred from five states. We take the state with the most candy
because: from the specified S(xs,ys) to the specified T(xt,yt);
initialize dp When creating an array, because we are asking for the maximum value, first store a number as small as possible. (-inf, not 0/-1)
Initial position dp[xs][ys][0]=0; Find the k that satisfies the conditions with the smallest value of dp[xt][yt][k].
Due to: How long does it take to get at least C candies on the way?
We consider the minimum time, (the dp value is the most currently obtained candy C), and the subscript is the position ij, and the required time k; we only need to traverse k from small to large, and get the dp value>=c, so that the time is the smallest

It feels a bit similar to the dp of the 01 backpack. The 01 backpack is, at the current moment, when the weight of the load is j, the value (dp value) is the largest
, so we initially want to make the dp as small as possible
*/

AC code

#include<bits/stdc++.h>
using namespace std;
const int maxn=10181;
int n,m,xs,xt,ys,yt,c;
int dp[15][15][maxn],t[15][15];
int maxx(int a,int b,int c,int d,int e)
{
    return max(a,max(b,max(c,max(d,e))));
}
int main()
{
    cin>>n>>m>>c;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>t[i][j];
        }
    }
    cin>>xs>>ys>>xt>>yt;
    memset(dp,-0x3f,sizeof(dp));
    dp[xs][ys][0]=0;
    for(int k=1;k<maxn;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                dp[i][j][k]=maxx(dp[i-1][j][k-1],dp[i+1][j][k-1],dp[i][j][k-1],dp[i][j-1][k-1],dp[i][j+1][k-1]);
                if(k%t[i][j]==0) dp[i][j][k]++;
            }
        }
    }
    for(int k=0;k<maxn;k++)
    {
        if(dp[xt][yt][k]>=c)
        {
            cout<<k<<endl;
            break;
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325736331&siteId=291194637