当前位置:网站首页>牛客网:过河卒
牛客网:过河卒
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;
}
边栏推荐
- Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
- Exercise 9-3 plane vector addition
- Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
- Page generation QR code
- Exercise 8-7 string sorting
- Solve the problem of dormitory router campus network sharing login
- GoLand 2021.1: rename the go project
- Use vscode to view hex or UTF-8 codes
- Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride( 金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
猜你喜欢
Example analysis of QT learning 18 login dialog box
[email "/>
Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
Using registered classes to realize specific type matching function template
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
消息订阅与发布
7-10 calculate salary
Exercise 10-1 judge the three digits that meet the conditions
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
Interface for querying IP home
MySQL data processing value addition, deletion and modification
随机推荐
JVM runtime data area
Qt学习18 登录对话框实例分析
Go 1.16.4: purpose of go mod tidy
Exercise 10-1 judge the three digits that meet the conditions
Fabric. JS document
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
如何使用lxml判断网站公告是否更新
Exercise 6-2 using functions to sum special A-string sequences
Qt学习17 对话框及其类型
Redis:字符串类型数据的操作命令
Current situation, analysis and prediction of information and innovation industry
Spring cup eight school league
1px problem of mobile terminal
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
Conversion function and explicit
Solution to failure or slow downloading of electron when electron uses electron builder to package
How to promote the progress of project collaboration | community essay solicitation
关于回溯问题中的排列问题的思考(LeetCode46题与47题)