当前位置:网站首页>822. Walk the Grid
822. Walk the Grid
2022-07-31 00:57:00 【Hunter_Kevin】
题目
给定一个 n×m 的方格阵,沿着方格的边线走,从左上角 (0,0) 开始,每次只能往右或者往下走一个单位距离,问走到右下角 (n,m) 一共有多少种不同的走法.
输入格式
共一行,包含两个整数 n 和 m.
输出格式
共一行,包含一个整数,表示走法数量.
数据范围
1≤n,m≤10
输入样例:
2 3
输出样例:
10
深搜代码AC
#include <iostream>
using namespace std;
int n, m;
int res;
void dfs(int x, int y)
{
if(x == n && y == m)res++;//If the current position is at the end point
else
{
if(x < n) dfs(x+1,y);//如果可以往下走
if(y < m) dfs(x, y+1);//Go right if you can
}
}
int main()
{
cin >> n >> m;
dfs(0,0);
cout << res << endl;
return 0;
}
Array simulation wide search,爆内存
#include <iostream>
using namespace std;
const int N = 15;
int dx[] = {
0,1}, dy[] = {
1,0};
int bfs(int x, int y)
{
int res = 0;
int q[1000][2] = {
0};
int f = 0, t = 0;
q[t][0] = 0, q[t][1] = 0;
t++;
while(f != t)
{
int curX = q[f][0], curY = q[f][1];
f++;
if(curX == x && curY == y)res++;
for(int i = 0; i < 2; i++)
{
int tX = curX + dx[i], tY = curY + dy[i];
if(tX >= 0 && tX <= x && tY >= 0 && tY <= y)
{
q[t][0] = tX, q[t][1] = tY;
t++;
}
}
}
return res;
}
int main()
{
int n, m;
cin >> n >> m;
cout << bfs(n,m) << endl;
return 0;
}
边栏推荐
- ros2知识:在单个进程中布置多个节点
- Can deep learning solve the parameters of a specific function?
- 【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
- 场景之多数据源查询及数据下载问题
- 响应式布局与px/em/rem的比对
- 射频器件的基本参数1
- typescript12 - union types
- Error occurred while trying to proxy request The project suddenly can't get up
- 【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
- 【952. Calculate the maximum component size according to the common factor】
猜你喜欢
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
Can deep learning solve the parameters of a specific function?
【Multithreading】
系统设计.短链系统设计
Gabor filter study notes
Rocky/GNU之Zabbix部署(3)
程序员工作三年攒多少钱合适?
BOM系列之history对象
Summary of MySQL database interview questions (2022 latest version)
typescript10-commonly used basic types
随机推荐
Niuke.com question brushing training (4)
论文理解:“Designing and training of a dual CNN for image denoising“
ShardingSphere之水平分库实战(四)
无线模块的参数介绍和选型要点
Responsive layout vs px/em/rem
Artificial Intelligence and Cloud Security
权限管理怎么做的?
Shell编程之条件语句
什么是Promise?Promise的原理是什么?Promise怎么用?
typescript11-数据类型
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
分布式.分布式锁
【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
Error in go mode tidy go warning “all” matched no packages
MySQL高级-六索引优化
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
typescript15-(同时指定参数和返回值类型)
mysql索引失效的常见9种原因详解