[Notes] do question Luo Gu P1002 Guohe Cu

Although entry is dp problem, but there are still many details that need attention


Assuming that \ (f_ {x, y} \) as the target location for \ ((x, y) \ ) several when walking, the answer is \ (f_ {n, m} \)

Without considering the bird nasty case of horses, for any coordinate \ ((i, J) \) , taking the number of programs it should be coming from above and to the left, that is, \ (f_ {i-1, j} \ + \ f_ {i , j-1} \) values (the number of programs coming from above and from the left and approaching).

At this point the sky bang, a horse debut, not difficult to find from the title given in the figure, only nine horses affect the value of the position, so can not go Guohe Cu

In other words, the horse can go places, Guohe Cu statistics should skip.

So an obvious idea: set up an array \ (s \) , before the start of the initialization dp \ (s \) array, the horse may come to mark the coordinates marked, and then when dp If the current coordinate this mark on \ (\ mathtt {Continue} \) , or order \ (F_ {I, J} \ = \ F_ {I-. 1, J} \ + \ F_ {I, J-. 1} \) , initialization \ (f_ {1 , 1} \) of \ (1 \) .

However NOI plus the question of who would be so conscience it? ? ? Obviously very cold heart. If so dry, so dry the outset, it will lead to initialize \ (f \) values in the array is to kill, the final result is \ (0 \) . It is necessary to determine what size of both, whichever is greater, to avoid this problem, i.e. \ (f_ {i, j} \ = \ \ max (f_ {i, j}, f_ {i-1, j} + f_ {i, j-1 }) \)

However NOI plus the topic of man's heart is just so cool it? ? ? Obviously much cooler than this. Since the transfer equation designed subtraction will lead to a non-existent access to an array of negative subscript! ! ! In fact, the solution is very simple, that is, to move down the board.

Finally, remember to open the \ (\ {mathtt Long \ Long} \) ! ! !

Reference Code

#include <iostream>
#include <stdio.h>
#include <math.h>
#define ll long long

using namespace std;

ll f[101][101],n,m,mn,mm,s[230][230];

void stop(ll x,ll y)
{
    s[x][y]=1;
    s[x-1][y-2]=1;
    s[x-2][y-1]=1;
    s[x-2][y+1]=1;
    s[x-1][y+2]=1;
    s[x+1][y-2]=1;
    s[x+2][y-1]=1;
    s[x+1][y+2]=1;
    s[x+2][y+1]=1;
}

//初始化 s 数组

int main()
{
    scanf("%lld%lld%lld%lld",&n,&m,&mn,&mm);

    n+=2,m+=2,mn+=2,mm+=2; //坐标下移

    stop(mn,mm);

    f[2][2]=1; //初始化下移

    for(int i=2;i<=n;i++) //棋盘下移
    {
        for(int j=2;j<=m;j++) //棋盘下移
        {
            if(s[i][j]) continue;
            f[i][j]=max(f[i][j],f[i-1][j]+f[i][j-1]);
        }
    }
    printf("%lld\n",f[n][m]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Nicest1919/p/12322417.html