洛谷P1002_过河卒_dp路径条数

题目大意:在棋盘n*m,卒起点在(0, 0)只能向下或者向右走,要走到终点(n, m),在(hx, hy)处有一个马,马走日,被马控制的格子都不能走,输入n m hx hy,问卒的所有路径条数

mark[][]标记马控制的格子
dp[i][j]表示走到(i, j)点的路径条数,初始化为0,dp[0][0]初始化为1。
状态转移方程:dp[i][j] += dp[i-1][j], dp[i][j] += dp[i][j-1]


#include 

using namespace std;

typedef long long LL;

int nx, ny, hx, hy;
LL dp[21][21] = {1};
bool mark[21][21] = {0};

int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};

int main()
{
    int i, j;
    scanf("%d %d %d %d", &nx, &ny, &hx, &hy);
    mark[hx][hy] = 1;
    for(i = 0; i < 8; ++i)
        if(hx+dx[i]>=0 && hx+dx[i]<=nx && hy+dy[i]>=0 && hy+dy[i]<=ny)
            mark[hx+dx[i]][hy+dy[i]] = 1;
    for(i = 0; i <= nx; ++i)
        for(j = 0; j <= ny; ++j)
    {
        if(mark[i][j]) continue;
        if(i) dp[i][j] += dp[i-1][j];
        if(j) dp[i][j] += dp[i][j-1];
    }
    printf("%lld\n", dp[nx][ny]);
    return 0;
}

你可能感兴趣的:(洛谷P1002_过河卒_dp路径条数)