### Input values in matrix

how can I manually input values in the matrix like the given matrix below in the code?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` `````` #include #include #include using namespace std; int countPaths(vector> const &mat, int m, int n, int cost, unordered_map &lookup) { if (cost < 0) { return 0; } if (m == 0 && n == 0) { if (mat[0][0] - cost == 0) { return 1; } else { return 0; } } string key = to_string(m) + "|" + to_string(n) + "|" + to_string(cost); if (lookup.find(key) == lookup.end()) { if (m == 0) { lookup[key] = countPaths(mat, 0, n - 1, cost - mat[m][n], lookup); } else if (n == 0) { lookup[key] = countPaths(mat, m - 1, 0, cost - mat[m][n], lookup); } else { lookup[key] = countPaths(mat, m - 1, n, cost - mat[m][n], lookup) + countPaths(mat, m, n - 1, cost - mat[m][n], lookup); } } return lookup[key]; } int countPaths(vector> const &mat, int cost) { if (mat.size() == 0) { return 0; } int M = mat.size(); int N = mat[0].size(); unordered_map lookup; return countPaths(mat, M - 1, N - 1, cost, lookup); } int main() { vector> mat = { { 4, 7, 1, 6 }, { 5, 7, 3, 9 }, { 3, 2, 1, 2 }, { 7, 1, 6, 3 } }; int cost = 25; cout << "Total paths with cost " << cost << " are " << countPaths(mat, cost); return 0; } ``````
Perhaps something like:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include #include #include #include using Array = std::vector>; using Lookup = std::unordered_map; size_t countPaths(const Array& mat, size_t m, size_t n, int cost, Lookup& lookup) { if (cost < 0) return 0; if (m == 0 && n == 0) { if (mat[0][0] - cost == 0) return 1; return 0; } const auto key { std::to_string(m) + "|" + std::to_string(n) + "|" + std::to_string(cost) }; if (lookup.find(key) == lookup.end()) if (m == 0) lookup[key] = countPaths(mat, 0, n - 1, cost - mat[m][n], lookup); else if (n == 0) lookup[key] = countPaths(mat, m - 1, 0, cost - mat[m][n], lookup); else lookup[key] = countPaths(mat, m - 1, n, cost - mat[m][n], lookup) + countPaths(mat, m, n - 1, cost - mat[m][n], lookup); return lookup[key]; } size_t countPaths(const Array& mat, int cost) { if (mat.size() == 0) return 0; const auto M { mat.size() }; const auto N { mat[0].size() }; Lookup lookup; return countPaths(mat, M - 1, N - 1, cost, lookup); } int main() { /* Array mat { { 4, 7, 1, 6 }, { 5, 7, 3, 9 }, { 3, 2, 1, 2 }, { 7, 1, 6, 3 } }; */ Array mat; size_t r {}, c {}; int cost {}; std::cout << "Enter number of rows: "; std::cin >> r; std::cout << "Enter number of cols: "; std::cin >> c; std::cout << "Enter cost: "; std::cin >> cost; mat.resize(r); for (size_t rwn {}; auto& row : mat) { row.resize(c); ++rwn; for (size_t cln {}; auto& col : row) { std::cout << "Value for row " << rwn << " col " << ++cln << ": "; std::cin >> col; } } std::cout << "\nTotal paths with cost " << cost << " are " << countPaths(mat, cost); }``````

thank you so much sir!! :)
Topic archived. No new replies allowed.