题目链接: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

Sample Output

-

分析:组合数/dp

dp:

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define N 100
using namespace std; int main()
{
int T,i,n,j,dp[N][N],x,y; scanf("%d", &T); while(T--)
{
scanf("%d %d %d", &n,&x,&y);
memset(dp,,sizeof(dp)); for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==x&&j==y)///
dp[i][j]=;
else if(i==&&j==)///
dp[i][j]=;///1和2顺序反了,wa~wa~wa~
else
dp[i][j]=dp[i-][j]+dp[i][j-]; dp[i][j]%=;
} if(dp[n][n]==)
printf("-1\n");
else
printf("%d\n", dp[n][n]);
}
return ;
}

组合数:

 #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; long long c(long long n,long long m)
{
if(m > n/)
m = n-m;
long long a =, b =;
for(int i =; i <= m; i++)
{
a *= n-i+;
b *= i;
if(a % b ==)
{
a /= b;
b =;
}
}
return a/b;
}
int main()
{
int t, n, x, y;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &n, &x, &y);
if((x == && y == )||(x == n && y == n))
{
printf("-1\n");
continue;
}
else
{
long long a = c(n*-, n-);
long long b = c(n*-x-y, n-x) * c( x+y--, x-);
int ans = (a-b)%;
printf("%d\n", ans);
}
}
return ;
}

Contest - 多校训练(985专场) Problem C: 985的方格难题的更多相关文章

  1. 算法训练 A+B Problem

     算法训练 A+B Problem   时间限制:1.0s   内存限制:512.0MB      问题描述 输入A,B. 输出A+B. 输入格式 输入包含两个整数A,B,用一个空格分隔. 输出格式 ...

  2. 2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记

    2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记 题意 多测.每次给你一个数\(n\),你可以同时问无数 ...

  3. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  4. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  5. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

    HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...

  6. Problem H: 小火山的围棋梦想 多校训练2(小火山专场)

    题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...

  7. Problem A: 小火山的跳子游戏 多校训练2(小火山专场)(周期)

    题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1158&pid=0 zzuli 1905  题意:如果k=1的话是1,2,3,4. ...

  8. 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...

  9. 【2016多校训练4】Multi-University Training Contest 4

    1001  Another Meaning 题意:字符串A中包含的字符串B可以翻译或不翻译,总共有多少方案. 题解:动规,dp[i]表示A的第i位为止有多少方案. 转移方程: dp[i]=dp[i-1 ...

随机推荐

  1. Java Timer及TimerTarsk(摘自网络)

    Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务. 这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少.任务的调用通过起的子线程进 ...

  2. html5+css3学习笔记音频和视频

    格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG 4 9.0+ No No 5.0+ 3.0+ WebM No 4.0+ ...

  3. The server instance Witness rejected configure request; read its error log file for more information. The reason 1427, and state 31, can be of use for

    数据库服务器做了镜像之后,发现有错误信息 The server instance Witness rejected configure request; read its error log file ...

  4. 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】

    在上一篇教程项目管理实践[三]每日构建[Daily Build Using CruiseControl.NET and MSBuild] 中,我们讲解了如何使用CCNET+MSBuild来自动编译项目 ...

  5. C#生成word

    using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Co ...

  6. javascript 事件响应

    1.基本事件 2.点击事件 <html> <head> <script type="text/javascript"> function add ...

  7. 《剑指Offer》算法题——替换空格

    题目:请实现一个函数,将一个字符串中的空格替换成“ % 20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are % 20Happy. class Solution ...

  8. URLEncode和URLDecoder作用

    一.基本原理 对于URL传递到后台,会对其中的有些字符进行编码,以下是我百度到的一些资料. 网页中的表单使用POST方法提交时,数据内容的类型是 application/x-www-form-urle ...

  9. OPENWRT make menuconfig错误之一

    1.make menuconfig rm: cannot remove `tmp/.host.mk': Permission denied 退到trunk上级目录sudo chown -R 777 t ...

  10. 第一个WebAPI项目

    (1)新建一个ASP.NET MVC项目,取名为:MyMvcWebAPIDemo,项目类型选择WebAPI. (2)在Models中新增一个类,取名为:Product,作为我们要测试的实体模型.   ...