Leetcode 576. The number of out-of-bounds paths (DAY 48) ---- Dynamic programming learning period (too tm torture people, I can finally eat)

Original title

Insert picture description here



Code implementation (first brush self-solving)

int findPaths(int m, int n, int N, int i, int j){
    
    
    long dp1[53][53],dp2[53][53],I,J,K;
    long temp,count;
    unsigned long long sum = 0;
    memset(dp2,0,sizeof(dp2));
    dp2[i+1][j+1] = 1;  
    int move[4][2] = {
    
    {
    
    0,1},{
    
    1,0},{
    
    0,-1},{
    
    -1,0}};
    for(I=1;I<=N;I++)
    {
    
    
        temp = I%2;
        if(temp)    memset(dp1,0,sizeof(dp1));
        else    memset(dp2,0,sizeof(dp2));
        for(J=1;J<=m;J++)
        {
    
    
            for(K=1;K<=n;K++)
            {
    
    
                if(temp && dp2[J][K])
                {
    
    
                    for(count=0;count<4;count++)
                    {
    
    
                        dp1[J+move[count][0]][K+move[count][1]] = (dp1[J+move[count][0]][K+move[count][1]] + dp2[J][K]) % 1000000007;
                        if(!(J+move[count][0]) || J+move[count][0] == m+1 || !(K+move[count][1]) || K+move[count][1] == n+1)
                            sum += dp2[J][K];
                    }
                }
                else if(!temp && dp1[J][K])   
                {
    
    
                    for(count=0;count<4;count++)
                    {
    
    
                        dp2[J+move[count][0]][K+move[count][1]] = (dp2[J+move[count][0]][K+move[count][1]] + dp1[J][K]) % 1000000007;
                    if(!(J+move[count][0]) || J+move[count][0] == m+1 || !(K+move[count][1]) || K+move[count][1] == n+1)
                        sum += dp1[J][K] ;
                    }
                }
            }
        }
    }
    return sum % 1000000007;
}

Guess you like

Origin blog.csdn.net/qq_37500516/article/details/113846257