当前位置: 首页 > news >正文

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

今天学习背包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.mrgr.cn/news/29809.html

相关文章:

  • 【MYSQL】主从复制机制(图解)
  • c++写一个死锁并且自己解锁
  • 《实时流计算系统设计与实现》-Part 2-笔记
  • 机器学习—softmax的改进实现
  • 小面馆叫号取餐流程 佳易王面馆米线店点餐叫号管理系统操作教程
  • 阿里云通义大模型团队开源Qwen2.5-Coder:AI编程新纪元
  • [SIGGRAPH-24] CharacterGen
  • easy_cloudantivirus
  • 《微处理器系统原理与应用设计第十三讲》通用同/异步收发器USART轮询模式应用设计
  • 算法之搜索--最长公共子序列LCS
  • 剃(磨)前插齿刀设计计算开发第二步:
  • Element Plus图片上传组件二次扩展
  • Android 中音频焦点的使用场景及示例
  • ssh远程连接try1账号切换tips
  • Java 之多线程高级
  • 计算机毕业设计 家电销售展示平台的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 使用python 将world的题库导入某学习软件的模板
  • 2024年9月python二级易错题和难题大全(附详细解析)(二)
  • MatchRFG:引领MemeCoin潮流,探索无限增长潜力
  • 论文不会写?分享6款AI论文写作免费一键生成网站!
  • Python urllib
  • LinuxC++的UDP服务器和客户端通信
  • 钢索缺陷检测系统源码分享
  • 1×1卷积核【super star 卷积核】
  • 人工智能与机器学习原理精解【21】
  • 图文检索(2):Visual-Linguistic Dependency Encoding for Image-Text Retrieval