为什么我的网格旅行者回忆还在粘住?
我目前正在努力将记忆实施到网格旅行者问题中。看起来它应该起作用,但是它仍然坚持使用(18,18)之类的更大案例。我是否错过了一些东西,还是地图不是解决此类问题的正确选择?
PS,我仍然很陌生地使用地图。
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
uint64_t gridTravMemo(int m, int n, unordered_map<string, uint64_t>grid)
{
string key;
key = to_string(m) + "," + to_string(n);
if (grid.count(key) > 0)
return grid.at(key);
if (m == 1 && n == 1)
return 1;
if (m == 0 || n == 0)
return 0;
grid[key] = gridTravMemo(m-1, n, grid) + gridTravMemo(m, n-1, grid);
return grid.at(key);
}
int main()
{
unordered_map<string, uint64_t> gridMap;
cout << gridTravMemo(1, 1, gridMap) << endl;
cout << gridTravMemo(2, 2, gridMap) << endl;
cout << gridTravMemo(3, 2, gridMap) << endl;
cout << gridTravMemo(3, 3, gridMap) << endl;
cout << gridTravMemo(18, 18, gridMap) << endl;
return 0;
}
I'm currently working on implementing memoization into the Grid Traveler problem. It looks like it should work, but it's still sticking on bigger cases like (18,18). Did I miss something, or are maps not the right choice for this kind of problem?
P.S. I'm still very new at working with maps.
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
uint64_t gridTravMemo(int m, int n, unordered_map<string, uint64_t>grid)
{
string key;
key = to_string(m) + "," + to_string(n);
if (grid.count(key) > 0)
return grid.at(key);
if (m == 1 && n == 1)
return 1;
if (m == 0 || n == 0)
return 0;
grid[key] = gridTravMemo(m-1, n, grid) + gridTravMemo(m, n-1, grid);
return grid.at(key);
}
int main()
{
unordered_map<string, uint64_t> gridMap;
cout << gridTravMemo(1, 1, gridMap) << endl;
cout << gridTravMemo(2, 2, gridMap) << endl;
cout << gridTravMemo(3, 2, gridMap) << endl;
cout << gridTravMemo(3, 3, gridMap) << endl;
cout << gridTravMemo(18, 18, gridMap) << endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
记忆的搜索点是通过返回您计算的任何先前值来优化运行时间。这样,您可以达到
o(n*m)
的运行时而不是蛮力算法。但是,您将
unordered_map&lt; string,uint64_t&gt; grid
作为深度优先搜索的参数。您正在调用
grid [key] = gridtravmemo(m-1,n,grid) + gridtravmemo(m,n-1,grid);
这意味着您的搜索将分成两个分支。 但是,这两个分支中的网格
是不同的。这意味着可以在两个单独的分支中访问相同的状态,从而导致运行时更像o( 2^(n*m))
。当您测试18x18网格时,这肯定不会很快运行。
这相对容易修复。只需将
网格
声明为全局变量即可。这样,它的值可以在不同的分支之间使用。尝试这样的事情:
The point of memorized search is to optimize running time by returning any previous values that you have calculated. This way, instead of a brute force algorithm, you can reach a runtime of
O(N*M)
.However, you are passing your
unordered_map<string, uint64_t>grid
as a parameter for your depth-first search.You are calling
grid[key] = gridTravMemo(m-1, n, grid) + gridTravMemo(m, n-1, grid);
This means that your search is splitting into two branches. However, thegrid
in these two branches are different. This means that the same state can be visited in two separate branches, leading to a runtime more likeO(2^(N*M))
.When you're testing an 18x18 grid, this definitely will not run quickly enough.
This is relatively easy to fix. Just declare
grid
as a global variable. This way its values can be used between different branches.Try something like this: