Problem C: XueXX and Chessboard
Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 155 Solved: 45
[ Submit][ Status][ Web Board]
Description
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
题解:
本题是DP,状态转移方程是dp[i][j]=dp[i-1][j]+dp[i][j-1],只不过要加上许多判断,最后即可求出答案,要注意输入从1开始输入,并且dp[0][1]=1,这样才能使dp[1][1]赋值为1。
最后提交上去的时候一定不要忘了取模!!!
并且,dp的题也要注意边界情况,比如这题是1 1 1 1 1 这样的话就要特殊处理!!!!
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=-1;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
int n,m,k;
int dp[1005][1005];
const int mod=1000000007;
int main()
{
int o;
cin>>o;
while(o--)
{
cle(dp,0);
cin>>n>>m>>k;
int x,y;
rep(i,k)
{
cin>>x>>y;
dp[x][y]=INF;
}
dp[0][1]=1;
Rep(i,1,n)
{
Rep(j,1,m)
{
if(dp[i][j]==INF)continue;
if (dp[i-1][j]>=0&&dp[i][j-1]>=0)
{
dp[i][j]+=(dp[i-1][j]+dp[i][j-1]);
dp[i][j]%=mod;
}
if (dp[i-1][j]==INF&&dp[i][j-1]==INF)
{
dp[i][j]+=0;
}
if (dp[i-1][j]>=0&&dp[i][j-1]==INF)
{
dp[i][j]+=dp[i-1][j];
dp[i][j]%=mod;
}
if (dp[i-1][j]==INF&&dp[i][j-1]>=0)
{
dp[i][j]+=dp[i][j-1];
dp[i][j]%=mod;
}
}
}
if (dp[n][m]==INF)
puts("0");
else
printf("%d\n",dp[n][m]);
}
return 0;
}