当前位置:网站首页>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;
}
边栏推荐
- ShardingSphere's unsharded table configuration combat (6)
- typescript18-对象类型
- Adding, deleting, modifying and checking the foundation of MySQL
- [Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
- ShardingSphere之水平分库实战(四)
- 小黑leetcode之旅:104. 二叉树的最大深度
- 【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
- Error occurred while trying to proxy request The project suddenly can't get up
- MySQL Series 1: Account Management and Engine
- [In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
猜你喜欢

射频器件的基本参数1

typescript12-联合类型

ShardingSphere read-write separation (8)

【952. Calculate the maximum component size according to the common factor】

C language force buckles the rotating image of the 48th question.auxiliary array

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

小黑leetcode之旅:104. 二叉树的最大深度

【ABAP】MFBF过账到质量检验库存类型Demo

Error in go mode tidy go warning “all” matched no packages

MySQL——数据库的查,增,删
随机推荐
深度学习可以求解特定函数的参数么?
【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
Artificial Intelligence and Cloud Security
程序员工作三年攒多少钱合适?
ShardingSphere's unsharded table configuration combat (6)
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]
Adding, deleting, modifying and checking the foundation of MySQL
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
Shell编程之条件语句
Niuke.com question brushing training (4)
MySQL系列一:账号管理与引擎
24. Please talk about the advantages and disadvantages of the singleton pattern, precautions, usage scenarios
MySQL table design for message queue to store message data
typescript15-(同时指定参数和返回值类型)
Jmeter parameter transfer method (token transfer, interface association, etc.)
TypeScript在使用中出现的问题记录
【Demo】ABAP Base64加解密测试
xss bypass: prompt(1)
typescript16-void
【Multithreading】