• 985的方格难题(dp)


    http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1157&pid=2

    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
     
     
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include <iostream>
    #include<algorithm>
    #include<queue>
    #define N 10005
    using namespace std;
    int dp[40][40];
    int k = 1e9+7;
    
    int main()
    {
        int T, n, x, y;
    
        scanf("%d", &T);
    
        while(T --)
        {
            scanf("%d %d %d", &n, &x, &y);
    
            if((x==1&& y ==1) || (x==n && y==n))
            {
                printf("-1
    ");
                continue;
            }
    
            memset(dp, 0, sizeof(dp));
    
            dp[1][0] = 1;
    
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    if(i==x && j==y)
                        dp[i][j] = 0;
                    else
                       dp[i][j]=dp[i-1][j]+dp[i][j-1];
    
                    dp[i][j] %= k;
                }
            }
    
            printf("%d
    ", dp[n][n]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    微信小程序-----校园头条详细开发之注册登录
    微信小程序-----校园头条详细开发之首页
    day 99天
    day 98天
    day 97 VUE第一天
    day 96
    day 95天
    day 94 RestFramework序列化组件与视图view
    day 93 Restframwork
    day 92
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5733337.html
Copyright © 2020-2023  润新知