<p>Given an <code>m x n</code> integers <code>matrix</code>, return <em>the length of the longest increasing path in </em><code>matrix</code>.</p>

<p>From each cell, you can either move in four directions: left, right, up, or down. You <strong>may not</strong> move <strong>diagonally</strong> or move <strong>outside the boundary</strong> (i.e., wrap-around is not allowed).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/05/grid1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> matrix = [[9,9,4],[6,6,8],[2,1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest increasing path is <code>[1, 2, 6, 9]</code>.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/27/tmp-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> matrix = [[3,4,5],[3,2,6],[2,2,1]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>The longest increasing path is <code>[3, 4, 5, 6]</code>. Moving diagonally is not allowed.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[1]]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= matrix[i][j] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    void init_matrix(vector<vector<int>>& m) {
        for (int i = 0; i < row_cnt; ++i) {

            m.push_back(vector<int>{});
            for (int j = 0; j < col_cnt; ++j)
                m[i].push_back(-1);
        }
    }
    int longestIncreasingPath(vector<vector<int>>& matrix) {
        g_m = matrix;
        row_cnt = matrix.size();
        col_cnt = matrix[0].size();
        init_matrix(costs);
        //init_matrix(visited);

        for (int i = 0; i < row_cnt; ++i) {
            for (int j = 0; j < col_cnt; ++j) {
                dfs(i, j);
            }
        }

        return max_length + 1;
    }
    std::vector<pair<int, int>> getAdjs(int row, int col) {
        std::vector<pair<int, int>> res;
        int current_val = g_m[row][col];
        if (((row - 1) >= 0) && (g_m[row - 1][col] > current_val)) {
            res.push_back({row - 1, col});
        }
        if (((row + 1) < row_cnt) && (g_m[row + 1][col] > current_val)) {
            res.push_back({row + 1, col});
        }
        if (((col - 1) >= 0) && (g_m[row][col - 1] > current_val)) {
            res.push_back({row, col - 1});
        }
        if (((col + 1) < col_cnt) && (g_m[row][col + 1] > current_val)) {
            res.push_back({row, col + 1});
        }
        //std::cout << "get adjs for " << row << "," << col << std::endl;
        for (auto p : res)
            std::cout << p.first << "," << p.second << std::endl;
        return res;
    }

    int dfs(int row, int col) {
        if (costs[row][col] != -1)
            return costs[row][col];
        auto adjs = getAdjs(row, col);
        int max_cost = -1;
        for (auto cell : adjs) {
            int cost = dfs(cell.first, cell.second);
            if (cost > max_cost)
                max_cost = cost;
        }
        costs[row][col] = max_cost + 1;

        if (costs[row][col] > max_length)
            max_length = costs[row][col];

        //std::cout << "dfs for " << row << " ," << col << ":"
        //              << costs[row][col] << std::endl;
        return costs[row][col];
    }

private:
    vector<vector<int>> g_m;
    int row_cnt;
    int col_cnt;
    int max_length = 0;
    vector<vector<int>> costs;
};
36515