#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
 
int map[510][510];
int dp[510][510];
int row, col;
 
int dx[4= {-1100};
int dy[4= {00-11};
 
int safe(int x, int y)
{
    if(x >= 0 && y >= 0 && x < row && y < col)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
int DFS(int x, int y)
{
    if(x == row-1 && y == col-1)
    {
        return 1;
    }
    
    if(dp[x][y] != -1)
    {
        return dp[x][y];
    }
    
    dp[x][y] = 0;
    
    for(int i = 0; i < 4; i++)
    {
        int xpos = x+dx[i];
        int ypos = y+dy[i];
        
        if(safe(xpos, ypos) == 1)
        {
            if(map[x][y] > map[xpos][ypos])
            {
                dp[x][y] += DFS(xpos, ypos);
            }
        }
    }
    
    return dp[x][y];
}
 
int main(void)
{
//    freopen("B1520_input.txt", "r", stdin);
 
    cin >> row >> col;
 
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cin >> map[i][j];
            dp[i][j] = -1;
        }
    }
    
    cout << DFS(00);
    
    return 0;
}
cs

+ Recent posts