当前位置:网站首页>牛客网:过河卒
牛客网:过河卒
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;
}
边栏推荐
- [acnoi2022] guess numbers
- QT learning 17 dialog box and its types
- How to use lxml to judge whether the website announcement is updated
- JS download files through URL links
- Implementation of Muduo accept connection, disconnection and sending data
- 7-10 calculate salary
- Uniapp tips - scrolling components
- Exercise 7-6 count capital consonants
- 关于回溯问题中的排列问题的思考(LeetCode46题与47题)
- GoLand 2021.1: rename the go project
猜你喜欢
Example analysis of QT learning 18 login dialog box
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
Multi person collaborative data annotation based on Baidu brain easydata from scratch
Common network state detection and analysis tools
Summary of common error reporting problems and positioning methods of thrift
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
QT learning 19 standard dialog box in QT (top)
QT learning 25 layout manager (4)
Global event bus
Comprehensive case of MySQL data addition, deletion, modification and query
随机推荐
Why don't I have a rookie medal
Current situation, analysis and prediction of information and innovation industry
可编程逻辑器件软件测试
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
JS get DPI, PX to cm, cm to PX
Conversion function and explicit
JVM object lifecycle
JS input number and standard digit number are compared. The problem of adding 0 to 0
1px problem of mobile terminal
Qt学习24 布局管理器(三)
js . Find the first palindrome string in the array
小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
Summary of common error reporting problems and positioning methods of thrift
GoLand 2021.1: rename the go project
Canvas utility library fabric JS user manual
Use vscode to view hex or UTF-8 codes
JVM runtime data area
Function calling convention
“又土又穷”的草根高校,凭什么被称为“东北小清华”?
Print. JS -- web page file printing