<p>In a gold mine <code>grid</code> of size <code>m x n</code>, each cell in this mine has an integer representing the amount of gold in that cell, <code>0</code> if it is empty.</p>

<p>Return the maximum amount of gold you can collect under the conditions:</p>

<ul>
	<li>Every time you are located in a cell you will collect all the gold in that cell.</li>
	<li>From your position, you can walk one step to the left, right, up, or down.</li>
	<li>You can&#39;t visit the same cell more than once.</li>
	<li>Never visit a cell with <code>0</code> gold.</li>
	<li>You can start and stop collecting gold from <strong>any </strong>position in the grid that has some gold.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,6,0],[5,8,7],[0,9,0]]
<strong>Output:</strong> 24
<strong>Explanation:</strong>
[[0,6,0],
 [5,8,7],
 [0,9,0]]
Path to get the maximum gold, 9 -&gt; 8 -&gt; 7.
</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
<strong>Output:</strong> 28
<strong>Explanation:</strong>
[[1,0,7],
 [2,0,6],
 [3,4,5],
 [0,3,0],
 [9,0,20]]
Path to get the maximum gold, 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 15</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 100</code></li>
	<li>There are at most <strong>25 </strong>cells containing gold.</li>
</ul>

using pt = pair<int, int>;

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

bool reach[1 << 25];
int dp[1 << 25];

class Solution {
public:
    int getMaximumGold(vector<vector<int>>& grid) {
        vector<pt> gpositions;
        vector<int> weight;
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>> index(n, vector<int>(m, -1));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j]) {
                    index[i][j] = gpositions.size();
                    gpositions.emplace_back(i, j);
                    weight.push_back(grid[i][j]);
                }
            }
        }
        int g = gpositions.size();

        memset(reach, 0, sizeof(reach));
        memset(dp, 0, sizeof(dp));
        const int fmask = (1 << g) - 1;
        for (int i = 0; i < g; ++i) {
            int bit = 1 << i;
            int mask = fmask & (~bit);
            const auto& pos = gpositions[i];
            reach[mask] = true;
            dp[mask] |= bit;
        }
        for (int mask = fmask; mask >= 0; --mask) {
            if (!reach[mask]) continue;
            for (int i = 0; i < g; ++i) {
                if ((dp[mask] & (1 << i)) == 0) continue;
                int x = gpositions[i].first;
                int y = gpositions[i].second;
                for (int dir = 0; dir < 4; ++dir) {
                    int nx = x + dx[dir];
                    int ny = y + dy[dir];
                    if (!(0 <= nx && nx < n && 0 <= ny && ny < m)) continue;
                    int idx = index[nx][ny];
                    if (idx == -1) continue;
                    int bit = 1 << idx;
                    if ((mask & bit) == 0) continue;
                    int nmask = mask & (~bit);
                    reach[nmask] = true;
                    dp[nmask] |= bit;
                }
            }
        }
        int ans = 0;
        for (int mask = fmask; mask >= 0; --mask) {
            if (reach[mask]) {
                int s = 0;
                for (int i = 0; i < g; ++i) {
                    if ((mask & (1 << i)) == 0) {
                        s += weight[i];
                    }
                }
                ans = max(ans, s);
            }
        }
        return ans;
    }
};
173300