当前位置:网站首页>牛客网:过河卒
牛客网:过河卒
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;
}边栏推荐
- Exercise 10-1 judge the three digits that meet the conditions
- Go 1.16.4: purpose of go mod tidy
- Spring cup eight school league
- Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
- Use and design of Muduo buffer class
- 信创产业现状、分析与预测
- QT learning 23 layout manager (II)
- JS get DPI, PX to cm, cm to PX
- Implementation of Muduo accept connection, disconnection and sending data
- Selenium browser (1)
猜你喜欢

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

jvm-运行时数据区

Redis: redis data structure and key operation commands

Redis:字符串類型數據的操作命令

Exercise 7-6 count capital consonants

全局事件总线
[email "/>Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email

Conversion function and explicit

Rasp implementation of PHP

Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
随机推荐
Exercise 7-6 count capital consonants
Simulated access
Exercise 6-2 using functions to sum special A-string sequences
FPGA测试方法以Mentor工具为例
Global event bus
Too many files with unapproved license
7-10 calculate salary
How to delete an attribute or method of an object
Formation of mil-100 (FE) coated small molecule aspirin [email protected] (FE) | glycyrrhetinic acid modified metal organ
JS shift operators (< <,> > and > > >)
Go: send the get request and parse the return JSON (go1.16.4)
Spring cup eight school league
FPGA test method takes mentor tool as an example
Similarities and differences of sessionstorage, localstorage and cookies
JS matrix zero
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
JVM runtime data area
JS new challenges
Qt学习24 布局管理器(三)
Selenium browser (1)