当前位置:网站首页>牛客网:过河卒
牛客网:过河卒
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;
}边栏推荐
- JS shift operators (< <,> > and > > >)
- 玖逸云黑免费无加密版本源码
- Uniapp tips - scrolling components
- JS get DPI, PX to cm, cm to PX
- Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
- Exercise 8-7 string sorting
- Qt学习17 对话框及其类型
- 金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
- Configure stylelint
- 怎样删除对象的某个属性或⽅法
猜你喜欢

信创产业现状、分析与预测

Qt学习21 Qt 中的标准对话框(下)

JVM class loading

Qt学习25 布局管理器(四)

QT learning 20 standard dialog box in QT (middle)

Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
![Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:](/img/2f/33504391a661ecb63d42d75acf3a37.png)
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:

MySQL data processing value addition, deletion and modification

GoLand 2021.2 configure go (go1.17.6)

Article content typesetting and code highlighting
随机推荐
Thrift threadmanager and three monitors
QT learning 23 layout manager (II)
Qt学习25 布局管理器(四)
simpleParallax. JS (create poor visual effects for website pictures)
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
Exercise 10-1 judge the three digits that meet the conditions
Uniapp skills - dom display and hiding
虽然不一定最优秀,但一定是最努力的!
JS get DPI, PX to cm, cm to PX
Redis:Redis的数据结构、key的操作命令
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
Exercise 8-7 string sorting
怎样删除对象的某个属性或⽅法
Exercise 6-1 classify and count the number of characters
Conversion function and explicit
如何使用lxml判断网站公告是否更新
Folic acid modified metal organic framework (zif-8) baicalin loaded metal organic framework composite magnetic material (AU- [email
[acnoi2022] guess numbers
jvm-类加载