zzuliOJ 1894: 985的方格难题 【dp】

1894: 985的方格难题

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 369  Solved: 75

Description

985走入了一个n * n的方格地图,他已经知道其中有一个格子是坏的。现在他要从(1, 1)走到(n, n),每次只可以向下或者向右走一步,问他能否到达(n,n)。若不能到达输出-1,反之输出到达(n,n)的方案数。
 

Input

第一行输入一个整数t,代表有t组测试数据。
每组数据第一行输入三个整数n,x,y,分别代表方格地图的大小以及坏掉格子的位置。
注:1 <= t <= 20,1 <= n <= 30,1 <= x,y <= n。

Output

若可以到达(n,n)则输出方案数对1e9 + 7取余的结果,反之输出-1。
 

Sample Input

2
2 1 2
2 2 2 

Sample Output

1
-1

HINT

 

Source

hpu


如此水的DP比赛的时候竟然没有看出来,【(⊙﹏⊙)b】, 我竟然还用DFS跑。

#include <cstdio> 
#include <cstring> 
#include <algorithm> 
#define MAXN 105 
using namespace std; 
int main() { 
    long long  dp[MAXN][MAXN]; 
    int t, x, y, n; 
    scanf("%d", &t); 
    while (t--) { 
        memset(dp, 0, sizeof(dp)); 
        scanf("%d%d%d", &n, &x, &y); 
        for (int i = 1; i <= n; i++) { 
            for (int j = 1; j <= n; j++) { 
                if (i == x && j == y) dp[i][j] = 0; 
                else if (i == 1 && j == 1) dp[1][1] = 1; 
                else dp[i][j] = (dp[i-1][j] + dp[i][j-1])%1000000007; 
            } 
        } 
        if (!dp[n][n]) printf("-1
"); 
        else printf("%lld
", dp[n][n]); 
    } 
    return 0; 
} 


原文地址:https://www.cnblogs.com/cniwoq/p/6770853.html