洛谷p1002 & NOIP2002 过河卒问题 (js解法)

79 阅读1分钟

[NOIP2002 普及组] 过河卒

题目描述

棋盘上 AA 点有一个过河卒,需要走到目标 BB 点。卒行走的规则:可以向下、或者向右。同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。

棋盘用坐标表示,AA(0,0)(0, 0)BB(n,m)(n, m),同样马的位置坐标是需要给出的。

转存失败,建议直接上传图片文件

现在要求你计算出卒从 AA 点能够到达 BB 点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步。

输入格式

一行四个正整数,分别表示 BB 点坐标和马的坐标。

输出格式

一个整数,表示所有的路径条数。

样例 #1

样例输入 #1

6 6 3 3

样例输出 #1

6

提示

对于 100%100 \% 的数据,1n,m201 \le n, m \le 2000 \le 马的坐标 20\le 20

【题目来源】

NOIP 2002 普及组第四题

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('line', (input) => {
    // console.log(`接收到:${input}`);
    let arr = input.split(' ')
    arr = arr.map(item => {
        return +item
    })
    let [x0, y0, x1, y1] = arr;
    solution(x0, y0, x1, y1)
});

var solution = function (x0, y0, x1, y1) {
    var arr = [];
    for (let i = 0; i <= x0; i++) {
        let temp = []
        for (let j = 0; j <= y0; j++) {
            temp.push(1)
        }
        arr.push(temp)
    }
    arr[x1][y1] = 0;
    let dp = [[-1, -2], [-2, -1], [-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2]]
    for (let i = 0; i < dp.length; i++) {
        let dx = x1 + dp[i][0];
        let dy = y1 + dp[i][1];
        if (dx >= 0 && dx <= x0 && dy >= 0 && dy <= y0) {
            arr[dx][dy] = 0;
        }
    }
    for (let i = 0; i <= x0; i++) {
        for (let j = 0; j <= y0; j++) {
            if (i == 0 && j == 0) continue;
            if (arr[i][j] == 0) {
                continue;
            }
            if (i == 0) {
                arr[i][j] = arr[i][j - 1];
                continue;
            }
            if (j == 0) {
                arr[i][j] = arr[i - 1][j];
                continue;
            }
            arr[i][j] = arr[i - 1][j] + arr[i][j - 1]
        }

    }
    console.log(arr[x0][y0])
    return arr[x0][y0]
}
// solution(8,6,0,4)