算法笔记/USACO Guide GOLD金组DP 3. Paths on Grids

news/2024/9/19 6:39:51 标签: 笔记, c++, 算法, 学习, 经验分享

今天学习背包DP(Knapsack DP) 

是USACO Guide的DP章节中第三点

What is grid DP? -Summary

DP problems often involve a 2D grid where paths are analyzed. Movement is restricted to one direction on the x-axis and y-axis, typically starting from one corner and ending at another. The goal may be to count paths or find max/min values. Sub-problems are sub-rectangles of the grid. For instance, to count paths from 1,1 to N,M moving positively, we define dp[x][y] as the number of paths to x,y. The recurrence relation is dp[x][y] = dp[x-1][y] + dp[x][y-1], with dp[1][1] = 1. Understanding cell appending aids in forming the correct recurrence.

简单来说,DP 问题通常涉及分析路径的时候使用2D的数组。移动仅限于 x 轴和 y 轴上的一个方向,通常从一个角开始并在另一个角结束。目标可能是对路 径进行计数或查找最大/最小值。子问题是网格的子矩形。例如,为了计算从 1,1 到 N,M 正向移动的路径,我们将 dp[x][y] 定义为到 x,y 的路径数。递推关系为 dp[x][y] = dp[x-1][y] + dp[x][y-1],其中 dp[1][1] = 1。理解单元格附加有助于形成正确的规律。

代表题目Grip Paths

Consider an n×n grid whose squares may have traps. It is not allowed to move to a square with a trap.

Your task is to calculate the number of paths from the upper-left square to the lower-right square. You can only move right or down.

Input

The first input line has an integer n: the size of the grid.

After this, there are nnn lines that describe the grid. Each line has n characters: . denotes an empty cell, and * denotes a trap.

Output

Print the number of paths modulo 10^9+7 

Constraints

  • 1≤n≤1000 

Example

Input:

4
....
.*..
...*
*...

Output:

3
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

bool ok[1000][1000];
ll dp[1000][1000];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < n; j++) {
			if (s[j] == '.') ok[i][j] = true;
			else ok[i][j] = false;
		}
	}

	dp[0][0] = 1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (!ok[i][j]) dp[i][j] = 0;
			else {
				if (i > 0) dp[i][j] += dp[i - 1][j];
				if (j > 0) dp[i][j] += dp[i][j - 1];
				dp[i][j] %= 1000000007;
			}
		}
	}

	cout << dp[n - 1][n - 1] << "\n";

	return 0;
}

思路

In this problem, we are directly given a 2D grid of cells, and we have to count the number of paths from corner to corner that can only go down (positive yyy direction) and to the right (positive xxx direction), with a special catch. The path can't use a cell marked with an asterisk.

We come close to being able to use our original recurrence, but we have to modify it. Basically, if a cell (x,y)(x, y)(x,y) is normal, we can use the recurrence normally. But, if cell (x,y)(x, y)(x,y) has an asterisk, the dp-value is 000, because no path can end on a trap.

在这个问题中直接给我们了一个2D元网格,我们必须用一个特殊的判断方法计算从一个角落到另一个角落只能向下的路径数量(正 y方向)和向右(正 x 方向)。这路径不能使用标有星号的单元格。

我们已经接近能够使用原来的递归方式了,但是我们必须修改它。简单来速回,如果一个单元格 (x, y) 是正常的,我们可以使用递归通常情况下。但是,如果单元格 (x, y) 有星号,则 dp 值为 0,因为没有路径可能以陷阱(*)结束。


http://www.niftyadmin.cn/n/5665165.html

相关文章

3款免费的GPT类工具

前言 随着科技的飞速发展&#xff0c;人工智能&#xff08;AI&#xff09;的崛起与发展已经成为我们生活中不可或缺的一部分。它的出现彻底改变了我们与世界互动的方式&#xff0c;并为各行各业带来了前所未有的便利。 一、Kimi 网址&#xff1a;点我前往 国产AI模型Kimi是一…

[Python学习日记-25] 哈希(HASH)是个什么东西?

[Python学习日记-25] 哈希&#xff08;HASH&#xff09;是个什么东西&#xff1f; 简介 哈希的特性 哈希的用途 基于 HASH 的数据类型 简介 哈希&#xff08;Hash&#xff09;&#xff0c;也称为散列&#xff0c;或音译为哈希&#xff0c;是把任意长度的输入&#xff08;又…

Angular面试题三

一、请解释Angular中的依赖注入是什么&#xff0c;并简述其工作原理。 Angular中的依赖注入&#xff08;Dependency Injection, DI&#xff09;是一种软件设计模式&#xff0c;它允许一个类&#xff08;通常是组件或服务&#xff09;在需要时接收其依赖项&#xff0c;而不是在类…

C语言 | Leetcode C语言题解之第413题等差数列划分

题目&#xff1a; 题解&#xff1a; int numberOfArithmeticSlices(int* nums, int numsSize) {if (numsSize 1) {return 0;}int d nums[0] - nums[1], t 0;int ans 0;// 因为等差数列的长度至少为 3&#xff0c;所以可以从 i2 开始枚举for (int i 2; i < numsSize; i…

松散绑定是什么?

概念 比如我的yml中写的last-name,这个和lastName是一样的&#xff0c;-后面跟着的字母默认是大写的&#xff0c;这就是松散绑定 示例 类代码&#xff1a; public class Person {private String lastName;private Integer age;private Boolean happy;private Date birth;pr…

前端mock了所有……

目录 一、背景描述 二、开发流程 1.引入Mock 2.创建文件 3.需求描述 4.Mock实现 三、总结 一、背景描述 前提&#xff1a; 事情是这样的&#xff0c;老板想要我们写一个demo拿去路演/拉项目&#xff0c;有一些数据&#xff0c;希望前端接一下&#xff0c;写几个表格&a…

软件测试 BUG 篇

目录 一、软件测试的生命周期 二、BUG 1. bug的概念 2. 描述bug的要素 3. bug的级别 4. bug的生命周期 5. 与开发产生争执怎么办&#xff1f;&#xff08;面试高频考题&#xff09; 5.1 先检查自身&#xff0c;是否bug描述不清楚 5.2 站在用户角度考虑并抛出问题 5.3 …

charles抓包flutter

一&#xff0c;准备工作 在我的另一篇文章flutter Dio发送post请求-CSDN博客里面&#xff0c;直接复用一部分代码 该方法无需让手机安装charles的ca证书&#xff08;当然安装了也没事儿&#xff09;&#xff0c;也无需设置手机wifi的网络代理&#xff08;因为ca证书的内容和网…