当前位置:网站首页>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;
}
边栏推荐
- typescript10-commonly used basic types
- SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
- 4G通信模块CAT1和CAT4的区别
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
- 查看zabbix-release-5.0-1.el8.noarch.rpm包内容
- 小黑leetcode之旅:104. 二叉树的最大深度
- Mini Program - Global Data Sharing
- ShardingSphere's vertical sub-database sub-table actual combat (5)
- 程序员工作三年攒多少钱合适?
- Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
猜你喜欢
Rocky/GNU之Zabbix部署(3)
Responsive layout vs px/em/rem
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
In Google Cloud API gateway APISIX T2A and T2D performance test
ShardingSphere之未分片表配置实战(六)
Thesis understanding: "Designing and training of a dual CNN for image denoising"
What is Promise?What is the principle of Promise?How to use Promises?
BOM系列之Navigator对象
xss bypass: prompt(1)
Gabor filter study notes
随机推荐
不用Swagger,那我用啥?
深度学习可以求解特定函数的参数么?
分布式.分布式锁
typescript10-commonly used basic types
Unity2D horizontal version game tutorial 4 - item collection and physical materials
typescript11-数据类型
GO GOPROXY代理设置
WMware Tools installation failed segmentation fault solution
分布式.幂等性
【952. 按公因数计算最大组件大小】
MySQL筑基篇之增删改查
程序员工作三年攒多少钱合适?
Rocky/GNU之Zabbix部署(1)
系统设计.短链系统设计
ShardingSphere之读写分离(八)
MySQL triggers
【愚公系列】2022年07月 Go教学课程 013-常量、指针
GO GOPROXY proxy Settings
[C language course design] C language campus card management system
Adding, deleting, modifying and checking the foundation of MySQL