Guohe Cu solution to a problem of dynamic programming Luo Gu P1002

Topic links: https://www.luogu.com.cn/problem/P1002

Subject to the effect

On the board \ (A \) point a Guohe Cu, need to come target \ (B \) points. Death walking rules: can be down, or right. CC points on the board at the same time there is the other side of a horse, the point where the horse jumps and step up all the points called control points other horses. So called "horse stopped Guohe Cu."

Chessboard coordinate representation, \ (A \) point \ ((0, 0) \) , \ (B \) point \ ((n-, m) \) ( \ (n-\) , \ (m \) is not more than \ (20 \) is an integer), the position coordinates of the same horse that needs given.

Now ask you to calculate death from (A \) \ can reach the point \ (B \) the number of points of the path, the position of assuming that the horse is stationary, not a soldier walking horse go step by step.

Input Format

Four data line, respectively, \ (B \) coordinate point coordinates and horses.

Output Format

A data representing the number of all paths.

Sample input 1

6 6 3 3

Sample output 1

6

answer

First of all, it is easy to see that this is a dynamic programming problem.
We can make \ (f [i] [j ] \) represents from \ ((0,0) \) (the point at the top left) walked \ ((i, J) \) (the first \ (i \) line the first \ (j \) the number of program columns).
So, without taking into account the presence of horses, you can get the state transition equation is as follows:

  • If \ (i = 0 \) and \ (J = 0 \) , then \ (F [I] [J] =. 1 \) ;
  • Otherwise, if the \ (= 0 I \) , then \ (F [I] [J] = F [I] [J-. 1] \) ;
  • Otherwise, if the \ (J = 0 \) , then \ (F [I] [J] = F [. 1-I] [J] \) ;
  • Ina则, \ (F [I] [J] = F [I-1] [J] Tasu F [I] [J-1] \) .

However, there exist horse, left on the basis of the above conditions, we must first step judgment (and this step must be placed in front of the judge or):

  • If \ ((i, j) \ ) be within the scope of the horse cock, the \ (F [I] [J] = 0 \) .

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int n, m, x, y;
long long f[22][22];
int main() {
    cin >> n >> m >> x >> y;
    for (int i = 0; i <= n; i ++) {
        for (int j = 0; j <= m; j ++) {
            if (abs(x-i)==2 && abs(y-j)==1 || abs(x-i)==1 && abs(y-j)==2 || x==i && y==j) f[i][j] = 0;
            else if (i==0 && j==0) f[i][j] = 1;
            else if (i==0) f[i][j] = f[i][j-1];
            else if (j==0) f[i][j] = f[i-1][j];
            else f[i][j] = f[i-1][j] + f[i][j-1];
        }
    }
    cout << f[n][m] << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/11994433.html