当前位置:网站首页>Real topic of the 2020 Landbridge cup provincial competition - go square (dp/dfs)
Real topic of the 2020 Landbridge cup provincial competition - go square (dp/dfs)
2022-06-23 00:54:00 【Stephen_ Curry___】
Original link : Go through the squares
Topic overview :
There are some two-dimensional lattices in the plane .
These points are numbered just like a two-dimensional array , From the top to the bottom is 1 To That's ok , From left to right, the order is 1 To Column , Each dot can be represented by line number and column number .
Now there's a man standing at number one 1 Xing di 1 Column , To go to the first Xing di Column .
You can only go right or down .
Be careful , If the row number and column number are even , You can't go into this space .
Ask how many options .
Ideas
As soon as you see the problem maze algorithm to find the maximum and minimum value, you must first consider whether to use dynamic programming to solve , Obviously, the question can be used DP Work it out , besides , If you can't use dynamic programming, you should consider using DFS perhaps BFS solve .
First , This topic uses DP The solution is very simple , It's a simple two-dimensional DP. We define dp[i][j] Go to the first place i Xing di j Number of options listed , And you can only go right or down at a time , From this we can write the state transition equation dp[i][j]=dp[i-1][j]+dp[i][j-1]( From left and up to the current position ) Particular attention : When i And j Even numbers dp[i][j]=0
DP Code :
#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int dp[N][N];
int n,m;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)dp[i][1]=1;
for(int j=1;j<=m;j++)dp[1][j]=1;
for(int i=2;i<=n;i++)
{
for(int j=2;j<=m;j++)
{
if(i%2==0&&j%2==0)dp[i][j]=0;
else dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
cout<<dp[n][m]<<endl;
}
except DP In addition to the solution, you can also use extensive search to record the total number of schemes ,DFS The code is as follows :
#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int mp[N][N];
int vis[N][N];
int dx[2]={
0,1};
int dy[2]={
1,0};
int n,m,ans;
bool in(int x,int y)
{
if(x<1||x>n||y<1||y>m)return false;
else return true;
}
void dfs(int x,int y)
{
if(x==n&&y==m)
{
ans++;
return;
}
vis[x][y]=1;
for(int i=0;i<2;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(in(nx,ny)&&!vis[nx][ny]&&mp[nx][ny]==0)
dfs(nx,ny);
}
vis[x][y]=0;
}
int main()
{
cin>>n>>m;
ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(i%2==0&&j%2==0)mp[i][j]=1;
else mp[i][j]=0;
dfs(1,1);
cout<<ans<<endl;
}
Blue Bridge Cup provincial tournament is coming , If you are reading this article, you are also preparing for the Blue Bridge Cup ^ - ^, Come on, everybody !!!
边栏推荐
- Daily question brushing record (I)
- How to refine permissions to buttons?
- Cadence spb17.4 - Chinese UI settings
- [initial launch] there are too many requests at once, and the database is in danger
- 关于测试/开发程序员技术的一些思考,水平很高超的,混不下去了......
- Swiftui swift tutorial 14 useful array operators
- TiDB VS MySQL
- How to get started with machine learning?
- Get the direction of mouse movement
- Because I said: volatile is a lightweight synchronized, the interviewer asked me to go back and wait for the notice!
猜你喜欢

外包干了四年,感觉废了..

OpenCvSharp (C# OpenCV) 微信QRCode解码功能使用介绍(附源码)

2022 TIANTI match - National Finals rematch

Have you stepped on these pits? Use caution when creating indexes on time type columns
Voice network multiplayer video recording and synthesis support offline re recording | Nuggets technology solicitation

3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World

E-R图

二叉树转字符串及字符串转二叉树

Ros2 summer school 2022 transfer-

黄金etf持仓量如何算
随机推荐
Have you stepped on these pits? Use caution when creating indexes on time type columns
Using geTx to build a more elegant structure of flutter pages
MGRE环境下的OSPF实验
美团基于 Flink 的实时数仓平台建设新进展
Learn the specific technical learning experience of designing reusable software from the three levels of class, API and framework
Brief introduction: how much do you know about fishing attacks
Which brokerage platform is better and safer for a brokerage to open an account on a mobile phone? What if you need a low commission
Tidb monitoring upgrade: a long way to solve panic
Ansible 学习总结(8)—— Ansible 控制提权相关知识总结
Ansible 学习总结(7)—— Ansible 状态管理相关知识总结
How to set the power-off auto start of easycvr hardware box
EasyCVR硬件盒子如何设置断电自启动
Typecho imite le modèle de thème du blog Lu songsongsong / modèle de thème du blog d'information technologique
【UVM】别再说你的 VIP 用不了 RAL Model
OSPF experiment in mGRE environment
Shell logs and printouts
初学者如何快速入门深度学习?
3 big questions! Redis cache exceptions and handling scheme summary
I've been outsourcing for four years, but I feel it's useless
ROS1Noetic在Win11中安装记录