zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    [整理]Cadence 生成带有网络追踪的 PDF 原理图
    [整理]FSM 有限状态机
    [原创]Quartus 中调用 Modelsim 波形仿真
    [原创]SPI 协议介绍以及基于 Verilog 的 IP 核实现
    [原创]Verilog 代码编程规范(个人用)
    [算法]线段树
    [算法]tarjan
    poj3280
    poj 3258 River Hopscotch
    [poj 1251]Jungle Roads
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5733337.html
Copyright © 2011-2022 走看看