[C++] 5 Dimension Top-Down DP
\nclass Solution {\npublic:\n    int memo[5][6][5][6][301];\n    int n;\n    int minimumDistance(string s) {\n        n = s.size();\n        memset(memo, -1, si
```\nclass Solution {\npublic:\n    int memo[5][6][5][6][301];\n    int n;\n    int minimumDistance(string s) {\n        n = s.size();\n        memset(memo, -1, sizeof(memo));\n        int res = 1e9;\n        for(int i = 0; i < 26; i++)\n            for(int j = 0; j < 26; j++)\n                res = min(res, dp(s, 0, i / 6, i % 6, j / 6, j % 6));\n        return res;\n    }\n    int dp(string& s, int idx, int r1, int c1, int r2, int c2){\n        if(idx == n)\n            return 0;\n        if(memo[r1][c1][r2][c2][idx] != -1)\n            return memo[r1][c1][r2][c2][idx];\n        int a = s[idx] - \'A\', r = a / 6, c = a % 6;\n        return memo[r1][c1][r2][c2][idx] = min(abs(r - r1) + abs(c - c1) + dp(s, idx + 1, r, c, r2, c2), abs(r - r2) + abs(c - c2) + dp(s, idx + 1, r1, c1, r, c));\n    }\n};\n```
['Dynamic Programming', 'Memoization', 'C', 'C++']