当前位置:网站首页>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;
}
边栏推荐
- Typescript14 - (type) of the specified parameters and return values alone
- Gabor filter study notes
- Shell programming of conditional statements
- Error occurred while trying to proxy request The project suddenly can't get up
- [In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
- C语言力扣第48题之旋转图像。辅助数组
- typescript18-对象类型
- typescript17-函数可选参数
- Meeting OA project pending meeting, all meeting functions
- Jetpack Compose learning (8) - State and remeber
猜你喜欢

Jmeter parameter transfer method (token transfer, interface association, etc.)

WMware Tools installation failed segmentation fault solution

Asser uses ant sword to log in
Go 学习笔记(84)— Go 项目目录结构

Detailed explanation of 9 common reasons for MySQL index failure

【c语言课程设计】C语言校园卡管理系统

ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)

Strict Mode for Databases

MySQL数据库进阶篇

DOM系列之 offset 系列
随机推荐
一万了解 Gateway 知识点
Error occurred while trying to proxy request项目突然起不来了
Basic usage of async functions and await expressions in ES6
typescript18-对象类型
typescript15-(同时指定参数和返回值类型)
什么是Promise?Promise的原理是什么?Promise怎么用?
Consistency and Consensus of Distributed Systems (1) - Overview
查看zabbix-release-5.0-1.el8.noarch.rpm包内容
typescript17-函数可选参数
MySQL的触发器
Yolov7实战,实现网页端的实时目标检测
typescript13-类型别名
ELK部署脚本---亲测可用
【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
Unity2D horizontal version game tutorial 4 - item collection and physical materials
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
网站频繁出现mysql等数据库连接失败等信息解决办法
XSS related knowledge
ShardingSphere之读写分离(八)
SWM32 Series Tutorial 6 - Systick and PWM