当前位置:网站首页>822. 走方格
822. 走方格
2022-07-31 00:51:00 【Hunter_Kevin】
822. 走方格
题目
给定一个 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++;//如果当前位置在终点
else
{
if(x < n) dfs(x+1,y);//如果可以往下走
if(y < m) dfs(x, y+1);//如果可以往右走
}
}
int main()
{
cin >> n >> m;
dfs(0,0);
cout << res << endl;
return 0;
}
数组模拟广搜,爆内存
#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;
}
边栏推荐
- SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
- (5) fastai application
- MySQL高级-六索引优化
- [In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
- 24. Please talk about the advantages and disadvantages of the singleton pattern, precautions, usage scenarios
- XSS related knowledge
- The client series of the DOM series
- unity2D横版游戏教程4-物品收集以及物理材质
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- typescript15-(同时指定参数和返回值类型)
猜你喜欢
mysql主从复制及读写分离脚本-亲测可用
小程序-全局数据共享
这个项目太有极客范儿了
MySQL database advanced articles
Shell programming of conditional statements
Unity2D horizontal version game tutorial 4 - item collection and physical materials
Preparations for web vulnerabilities
typescript10-常用基础类型
Typescript18 - object type
ShardingSphere's unsharded table configuration combat (6)
随机推荐
Regular expression password policy and regular backtracking mechanism bypass
24. Please talk about the advantages and disadvantages of the singleton pattern, precautions, usage scenarios
Typescript18 - object type
The difference between h264 and h265 decoding
Jmeter参数传递方式(token传递,接口关联等)
MySQL数据库面试题总结(2022最新版)
ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)
分布式系统的一致性与共识(1)-综述
Artificial Intelligence and Cloud Security
WEB安全基础 - - -漏洞扫描器
typescript9-常用基础类型
MySQL database advanced articles
redis学习
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
Rocky/GNU之Zabbix部署(1)
Responsive layout vs px/em/rem
Go study notes (84) - Go project directory structure
typescript9 - common base types
Asser uses ant sword to log in
DOM系列之scroll系列