DP——过河卒
Description
棋盘上A点有一个过河卒,需要走到目标B点。卒行走的规则:可以向下、或者向右。同时在棋盘上的任一点有一个对方的马(如C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点,如图中的C点和P1,……,P8,卒不能通过对方马的控制点。棋盘用坐标表示,A点(0,0)、B点(n, m) (n,m为不超过20的整数),同样马的位置坐标是需要给出的,C≠A且C≠B。现在要求你计算出卒从A点能够到达B点的路径的条数。
1 2 3 4 5 6 7 8 A +---+---+---o---+---o---+---+----+--------->Y | | | | | | | | | 1 +---+---o---+---+---+---o---+----+ | | | | | | | | | 2 +---+---+---+---C---+---+---+----+ | | | | | | | | | 3 +---+---o---+---+---+---o---+----+ | | | | | | | | | 4 +---+---+---o---+---o---+---+----+ B(4,8) | | V X
Input
多个测试案例,每个案例一行,处理到文件末尾 B点坐标(n,m)以及对马的坐标(X,Y){不用判错}
Output
一个整数(路径的条数)
Sample Input
6 6 3 2
Sample Output
17
大意:从起点开始到终点进行DP
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> using namespace std; int n,m,x,y; long long dp[50][50]; int main() { int i,j; while(~scanf("%d%d%d%d",&m,&n,&x,&y)) { for(int i = 0 ; i <= m+1;i++) for(int j = 0 ; j <= n+1;j++) dp[i][j] = 0; dp[x-2+1][y-1+1] = dp[x-2+1][y+1+1] = dp[x+1][y+1] = dp[x-1+1][y-2+1] = dp[x-1+1][y+2+1] = dp[x+1+1][y+2+1] = dp[x+1+1][y-2+1] = dp[x+2+1][y+1+1] = dp[x+2+1][y-1+1] = -1; dp[1][1] = 1; for(int i = 1; i <= m + 1;i++) for(int j =1 ;j <= n +1 ;j++) { if(dp[i][j] == -1) continue; dp[i][j] += dp[i-1][j] + dp[i][j-1]; if(dp[i-1][j] == -1) dp[i][j]++; if(dp[i][j-1] == -1) dp[i][j]++; } printf("%lld\n",dp[m+1][n+1]); } return 0; }