当前位置:网站首页>牛客网:过河卒
牛客网:过河卒
2022-07-03 13:38:00 【lsgoose】



嘛,这题是个很明显的dp题目。
首先写出动态转移方程,设dp[i][j]表示坐标(0,0)到(i,j)的路径数,这个路径数等于其左边节点的路径数和上边节点的路径数之和,显然dp[i][j]=dp[i-1][j]+dp[i][j-1],这是这个点可以走的情况。当然,如果这个点是马可以到达的,那么直接dp[i][j]=0。
至于具体的实现:
1.在判断马点的时候,直接在遍历过程中判断坐标即可,之前的思路想着先在地图上面标记,这样的做法还是麻烦了点
2.在第一列和第一行时需要特殊赋值,坐标原点需要赋值1
3.至于数据大小我们算一下40里选20的组合数大约估计一下最大数量级,大概是137846528820,然后int的最大值是2147483647,很明显,这里我们要用更大的数据类型来存储答案,比如long long
代码如下所示:
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y;
//检查坐标是否是马可以到的点
bool check(int i, int j, int x, int y){
return (abs(x-i) + abs(y-j) == 3 && x!=i && y!=j) || (x==i && y==j);
}
long long dp(vector<vector<long long>> &mp){
mp[0][0]=1;
for(int i=1;i<=n;++i){
mp[i][0] = (check(i, 0, x, y)) ? 0 : mp[i-1][0];
}
for(int j=1;j<=m;++j){
mp[0][j] = (check(0, j, x, y)) ? 0 : mp[0][j-1];
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
mp[i][j] = (check(i, j, x, y)) ? 0 : mp[i-1][j] + mp[i][j-1];
}
}
return mp[n][m];
}
int main(){
cin>>n>>m>>x>>y;
vector<vector<long long>> mp(n+1, vector<long long>(m+1, 0));
cout<<dp(mp)<<endl;
return 0;
}边栏推荐
- Back to top implementation
- 好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
- Vite project commissioning
- allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
- Redis: commandes d'action pour les données de type chaîne
- Qt学习24 布局管理器(三)
- Common network state detection and analysis tools
- JS matrix zero
- Dlopen() implements dynamic loading of third-party libraries
- 28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
猜你喜欢

FPGA测试方法以Mentor工具为例

交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳

从零开始的基于百度大脑EasyData的多人协同数据标注

Go language web development series 30: gin: grouping by version for routing

Summary of common error reporting problems and positioning methods of thrift

关于回溯问题中的排列问题的思考(LeetCode46题与47题)

GoLand 2021.1: rename the go project

The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log

好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录

Comprehensive case of MySQL data addition, deletion, modification and query
随机推荐
Common plug-ins for vite project development
Uniapp skills - scrolling components -1
QT learning 17 dialog box and its types
Exercise 8-2 calculate the sum and difference of two numbers
js 2023. String pair equal to the target string after connection
7-9 find a small ball with a balance
GoLand 2021.1.1: configure the multi line display of the tab of the open file
Implementation of Muduo accept connection, disconnection and sending data
Failure of vector insertion element iterator in STL
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
JVM垃圾回收机
Comprehensive case of MySQL data addition, deletion, modification and query
Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides
Redis: operation command of string type data
Dynamic programming 01 knapsack and complete knapsack
Exercise 6-1 classify and count the number of characters
Use vscode to view hex or UTF-8 codes
Conversion function and explicit
Current situation, analysis and prediction of information and innovation industry
Qt学习22 布局管理器(一)