XueXX and Chessboard(中南OJ)

来源:互联网 发布:mac steam 游戏存档 编辑:程序博客网 时间:2024/05/01 16:30

Problem C: XueXX and Chessboard

Time Limit: 1 Sec  Memory Limit: 512 MB
Submit: 155  Solved: 45
[Submit][Status][Web Board]

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, y(1 <= x<= 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

35 5 01 1 03 3 12 2

Sample Output

7012

题解:

本题是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;}

0 0
原创粉丝点击