XueXX and Chessboard(中南OJ)

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

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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值