当前位置:网站首页>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;
}
边栏推荐
- MySQL triggers
- ShardingSphere's vertical sub-database sub-table actual combat (5)
- Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
- typescript16-void
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- MySQL database advanced articles
- The difference between h264 and h265 decoding
- 查看zabbix-release-5.0-1.el8.noarch.rpm包内容
- This project is so geeky
- ELK部署脚本---亲测可用
猜你喜欢

场景之多数据源查询及数据下载问题
Go 学习笔记(84)— Go 项目目录结构

程序员工作三年攒多少钱合适?

Kotlin协程:协程上下文与上下文元素

MySQL数据库进阶篇

Unity2D horizontal version game tutorial 4 - item collection and physical materials

ShardingSphere之读写分离(八)

Error occurred while trying to proxy request The project suddenly can't get up

Regular expression password policy and regular backtracking mechanism bypass

BOM系列之history对象
随机推荐
MySQL笔记下
MySQL的触发器
WMware Tools安装失败segmentation fault解决方法
程序员工作三年攒多少钱合适?
Shell编程之条件语句
ShardingSphere read-write separation (8)
金融政企被攻击为什么要用高防CDN?
typescript15-(同时指定参数和返回值类型)
Strict Mode for Databases
Niuke.com question brushing training (4)
【ABAP】MFBF过账到质量检验库存类型Demo
Regular expression password policy and regular backtracking mechanism bypass
Jmeter参数传递方式(token传递,接口关联等)
redis学习
人工智能与云安全
WEB安全基础 - - -漏洞扫描器
深度学习可以求解特定函数的参数么?
WEB Security Basics - - - Vulnerability Scanner
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
In Google Cloud API gateway APISIX T2A and T2D performance test