【组队赛#7】BNU 4275 Your Ways(数学题 + 动态规划)

【题目链接】click here~~

【题目大意】:题意:给出一个w*h的方格,问除去不能走的路,从(0,0)到(w,h)共有多少种走法。

【解题思路】:第七场比赛的题,最后一小时在看这道题,比较遗憾最后还是没有A出来,赛后重新看了看题目,理清一下思路,发现就是道简单的dp,

处理一下除去不能走的路,不过要注意题目的一句话:“ The blocking is done in such a way that it isnot
possible to reach parts of the streets or avenues which is blocked from some other part which is blocked as well through any paths containingonly
West-to-East and South-to-North walks.”,
意思是说:阻塞的街道中分由西向东阻塞或者由南向北阻塞。一旦阻塞,意味着这条路不能通过,但是两边的点还是可以走的,阻塞的街道不影响其他可以走的街道。

因此,总的方案数如何求?用二维dp数组保存全部可以的方案, dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;(注意取模),那么总的方案数ans=dp[w][h],

不能走的方案数:res=dp[x1][y1]*dp[w-x2][h-y2](注意是乘!),最后答案就是(ans-res)%(mod)

代码:

#include <bits/stdc++.h>
using namespace std;
#define mod 2552
int t,w,h,k,x1,y1,x2,y2,ans,num;
int dp[1010][1010];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>w>>h>>k;
        int maxx=max(w,h);
        for(int i=0; i<=maxx; i++) dp[0][i]=dp[i][0]=1;//预处理
        for(int i=1; i<=w; i++)
            for(int j=1; j<=h; j++){
                dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;//总方案数
            }
           // cout<<dp[w][h]<<endl;;
        for(int i=1; i<=k; i++){
            ans=dp[w][h];
            cin>>num;
            for(int i=1; i<=num; i++){
                cin>>x1>>y1>>x2>>y2;
                ans-=dp[x1][y1]*dp[w-x2][h-y2];//总方案-阻塞的方案
                ans=(ans+mod)%mod;
            }
            if(ans<0)ans+=mod;
            printf("%d\n",ans);
        }
    }
    return 0;
}
时间: 2024-08-06 03:27:23

【组队赛#7】BNU 4275 Your Ways(数学题 + 动态规划)的相关文章

Bnuoj 4275 Your Ways(数学题 + 动态规划)

Your Ways You live in a small well-planned rectangular town in Phuket. The size of the central area of the town is H kilometers x W kilometers. The central area is divided into HW unit blocks, each of size 1 x 1 km2. There are H + 1 streets going in

Leetcode 动态规划 Decode Ways

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Decode Ways Total Accepted: 8689 Total Submissions: 55465 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a

【数学题-递推找规律】BNU 4225 杨辉三角形

[题目链接]click here~~ [题目大意] LZM同学比较牛,Lsy最近也越来越生猛,他们思路快,代码速度神勇.近期惊闻此二人均要参加校赛,队里决定出些题目卡他们,因为他们的罢工给题目组留下了繁重的负担--(报复报复) 于是,XsugarX瞄准了LZM不太喜欢看的数学题目以及Lsy猜公式的喜好,奸笑中(^.^).这个数学问题是个比较古老的问题,有如下图的三角形被称为杨辉三角形: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 我们记第一个1为第0行

hdu 1978 How many ways (动态规划、记忆化搜索)

How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2957    Accepted Submission(s): 1733 Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

LeetCode之“动态规划”:Decode Ways

题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Gi

leetcode_241——Different Ways to Add Parentheses (递归,动态规划)

Different Ways to Add Parentheses Total Accepted: 1708 Total Submissions: 6304My Submissions Question Solution Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and op

Decode Ways,编码方式数量求解。动态规划问题。

问题描述: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given en

【组队赛#5】BNU 4291 Arbitrage? (floyd最短路 map映射)

[题目链接]click here~~ [题目大意]去多个国家旅游,给定国与国之间汇率的转化率,如果从起点出发最后回到起点,有收益则符合,否则不符合 [解题思路] 判一次环,用floyd计算距离最短的而且转换率最大的,map<string ,int >映射  <字符串--点> 代码 /* BNUOJ 4291 Arbitrage? Author :HRW 判一次环,用floyd计算距离最短的而且转换率最大的 map<string ,int >映射 <字符串--点&g