当前位置:网站首页>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;
}
边栏推荐
- GO GOPROXY proxy Settings
- ELK部署脚本---亲测可用
- Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- Artificial Intelligence and Cloud Security
- 一万了解 Gateway 知识点
- Jmeter parameter transfer method (token transfer, interface association, etc.)
- C语言力扣第48题之旋转图像。辅助数组
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
- Can deep learning solve the parameters of a specific function?
- 深度学习可以求解特定函数的参数么?
猜你喜欢
随机推荐
typescript9-常用基础类型
MySQL database constraints, table design
MySQL的触发器
孩子的编程启蒙好伙伴,自己动手打造小世界,长毛象教育AI百变编程积木套件上手
Responsive layout vs px/em/rem
Can deep learning solve the parameters of a specific function?
Rocky/GNU之Zabbix部署(2)
ShardingSphere's public table combat (7)
A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
Oracle has a weird temporary table space shortage problem
Why use high-defense CDN when financial, government and enterprises are attacked?
Rocky/GNU之Zabbix部署(1)
SWM32 Series Tutorial 6 - Systick and PWM
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
typescript10-commonly used basic types
Artificial Intelligence and Cloud Security
35. 反转链表
射频器件的基本参数2
分布式.分布式锁
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators








