gpt4 book ai didi

algorithm - 格子路径内存

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:52 25 4
gpt4 key购买 nike

我正在尝试使用动态规划方法解决格子路径问题。

Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.

route

How many routes are there through a 2020 grid?

这是我为解决这个问题而编写的代码。我哪里错了。我似乎每次都得到错误的输出。我是否跨越了可变数据类型的界限?

#include <stdio.h>
int count = 0;
int limita,limitb;
long long int cache[20][20];
unsigned long long int start(int a,int b)
{
unsigned int long long i = 0;
if(a == limita && b == limitb)
return 1;
if(cache[a][b] != -1)
return cache[a][b];
if(a != limita)
i += start(a+1, b);
if(b != limitb)
i += start(a, b+1);
cache[a][b] = i;
return i;
}
int main(void)
{
limita = limitb = 19;
int i,j;
for(i = 0; i < 20; i++)
for(j = 0; j <20;j++)
cache[i][j] = -1;
unsigned long long int number = start(0,0);
printf("The number of ways to reach the end is %llu\n",number);
return 0;
}

请帮帮我

最佳答案

大小为1*1的网格:

    0    1
0+-----+
| |
| |
1+-----+
|<-2->|

大小为2*2的网格:

    0    1    2
0+----+----+
| | |
| | |
1+----+----+
| | |
| | |
2+----+----+
|<---3--->|

...

您的算法似乎没问题,但您计算的边数有误。

关于algorithm - 格子路径内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12454473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com