回忆的网格旅行解决方案作为常规递归解决方案运行

发布于 2025-01-23 03:17:38 字数 1822 浏览 3 评论 0 原文

问题陈述:

您在N Grid的M的左上角。如果您只能向下或右移动,您可以到达右下方的几种方法?

观察:

代码:

#include <iostream>
#include <cstring>
#include <map>
using namespace std;

string find_target(string target, map<string, unsigned>& v) {
    if (v.find(target) != v.end()) {
        cout << v[target] <<endl;
        return target;
    }
    return "0";
}

int gridTraveller(unsigned m, unsigned n, map<string, unsigned>& v) {
    
    string a =  m + "," + n;
    a = find_target(a, v);

    // Debugging
    // cout << a << " ";

    if (strcmp(a.c_str(), "0")!=0) return v[a];
    if (m==1 && n==1) return 1;
    else if (m==0 || n==0) return 0;
    
    v[a] = gridTraveller(m-1, n, v) + gridTraveller(m, n-1, v);
    return v[a];
}

int main() {
    cout<<"Enter no of rows: ";
    unsigned row; cin >> row;
    cout<<"Enter no of columns: ";
    unsigned col; cin >> col;
    map <string, unsigned> visited;
    cout << gridTraveller(row, col, visited);
    return 0;
}

常规输出:

Enter no of rows: 3
Enter no of columns: 2
3

调试输出:

Enter no of rows: 3
Enter no of columns: 2
0 0 0 0 0 0 0 0 0 0 0 0 0 3

Problem Statement:

You are at the top left corner of an m by n grid. How many ways you can reach the bottom right box if you can only move Down or Right?

Observations:

Code:

#include <iostream>
#include <cstring>
#include <map>
using namespace std;

string find_target(string target, map<string, unsigned>& v) {
    if (v.find(target) != v.end()) {
        cout << v[target] <<endl;
        return target;
    }
    return "0";
}

int gridTraveller(unsigned m, unsigned n, map<string, unsigned>& v) {
    
    string a =  m + "," + n;
    a = find_target(a, v);

    // Debugging
    // cout << a << " ";

    if (strcmp(a.c_str(), "0")!=0) return v[a];
    if (m==1 && n==1) return 1;
    else if (m==0 || n==0) return 0;
    
    v[a] = gridTraveller(m-1, n, v) + gridTraveller(m, n-1, v);
    return v[a];
}

int main() {
    cout<<"Enter no of rows: ";
    unsigned row; cin >> row;
    cout<<"Enter no of columns: ";
    unsigned col; cin >> col;
    map <string, unsigned> visited;
    cout << gridTraveller(row, col, visited);
    return 0;
}

Regular Output:

Enter no of rows: 3
Enter no of columns: 2
3

Debug Output:

Enter no of rows: 3
Enter no of columns: 2
0 0 0 0 0 0 0 0 0 0 0 0 0 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2025-01-30 03:17:38

找到了错误!事实证明,我对地图键有问题 - 在串联之前,我必须将 int 转换为 String 。还找到了一种更好的方法来检查键是否已经存在于地图中

string int_to_str(int x) {
   stringstream s;
   s << x;
   return s.str();
}

unsigned gridTraveller(unsigned m, unsigned n, map<string, unsigned>& v) {
    string a = int_to_str(m) + "," + int_to_str(n);
    if (v.count(a)) return v[a];
    ...
}

Found the error! Turns out I had a problem with the map keys - I had to convert the int to string before concatenation. Also found a better way to check if the key is already present in the map

string int_to_str(int x) {
   stringstream s;
   s << x;
   return s.str();
}

unsigned gridTraveller(unsigned m, unsigned n, map<string, unsigned>& v) {
    string a = int_to_str(m) + "," + int_to_str(n);
    if (v.count(a)) return v[a];
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文