P1002
标记马可以到达的地方,因为卒是能向下或向右走,设f[i][j]表示到达(i,j)的路径数,显然有:f[i][j] = f[i - 1][j] + f[i][j - 1]。
DP转移即可。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int n, m, a, b; 5 int dx[9] = {0, -2, -1, 1, 2, 2, 1, -1, -2}; 6 int dy[9] = {0, -1, -2, -2, -1, 1, 2, 2, 1}; 7 int mp[30][30]; 8 ll f[30][30];//路径很多,要开longlong 9 bool check(int x, int y) { 10 if ((x >= 1) && (x <= n) && (y >= 1) && (y <= m)) return 1; 11 else return 0; 12 } 13 int main() { 14 scanf("%d %d %d %d", &n, &m, &a, &b); 15 n ++, m ++, a ++, b ++;//下标从1开始 16 mp[a][b] = 1; 17 for (int i = 1; i <= 8; i ++) { 18 int nx = a + dx[i], ny = b + dy[i]; 19 if (check(nx, ny)) mp[nx][ny] = 1; 20 } 21 f[1][1] = 1; 22 for (int i = 1; i <= n; i ++) { 23 for (int j = 1; j <= m; j ++){ 24 if (i == 1 && j == 1) continue; 25 if (mp[i][j]) continue; 26 f[i][j] = f[i][j - 1] + f[i - 1][j]; 27 } 28 } 29 printf("%lld\n", f[n][m]); 30 return 0; 31 }
标签:过河,NOIP2002,int,ll,30,P1002,&& From: https://www.cnblogs.com/YHxo/p/16776405.html