Description
XueXX is a clever boy. And he always likes to do something with chessboard. What an interesting hobby!
Now XueXX is in a n*m chessboard, and he needs to go from (1,1) to (n,m). There are also some obstacles(障碍) in the chessboard, which he cannot move to. He wants to know how many ways he can get to the end point. Can you help him? Since the result is so huge, you need to mod the result by 1,000,000,007.
Input
The first line of input contains the number of test cases T. The descriptions of the test cases follow: The first line of each test case contains three integers n, m, k (1 <= n, m <= 1000, 0 < k <= 1000), respectively standing for the row number and the column number and the number of the obstacles. Then follows k lines , and each line contains two integer xi, yi (1 <= xi <= n, 1 <= yi <= m) , respectively standing for the coordinate(坐标) of the i-th obstacle.
Output
For each test case, output a single line containing the result (mod by 1,000,000,007).
Sample Input
3
5 5 0
1 1 0
3 3 1
2 2
Sample Output
70
1
2
HINT
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
#define N 1010
#define M 1000000007
long long dp[N][N],xi,yi;
int main(){
int t,n,m,i,j,k,flag;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&m,&k);
memset(dp,0,sizeof(dp));
// for(i=1;i<=n;i++){
// dp[i][1] = 1;
// }
// for(i=1;i<=m;i++){
// dp[1][i] = 1;
// }
dp[1][1] = 1;
while(k--){
scanf("%d %d",&xi,&yi);
dp[xi][yi] = -1;
}
flag = 1;
if(dp[1][1]==-1){
flag = 0;
dp[n][m] = 0;
}
for(i=1;flag && i<=n;i++){
for(j=1;j<=m;j++){
if(i==1&&j==1){
continue;
}
if(dp[i][j]==-1){
dp[i][j] = 0;
continue;
}
dp[i][j] = (dp[i-1][j] + dp[i][j-1])%M;
}
}
printf("%lld\n",dp[n][m]%M);
}
return 0;
}
本文介绍了一种使用动态规划算法解决棋盘上从起点到终点的路径计数问题的方法,考虑到棋盘上的障碍物限制,最终结果需要对1,000,000,007取模。
888

被折叠的 条评论
为什么被折叠?



