<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>

typedef struct{
    int x;
    int y;
}move_t;

class Solution {
public:

    //per ogni x e y differente d

    void dfs(int sum,int x,int y,vector<vector<bool>> &V,vector<vector<int>> &grid,int &max_sum){

        vector<move_t> candidates;
        
        move_t m;
        if((x+1)<=grid.size()-1 && V[x+1][y] == false && grid[x+1][y] != 0){
            m.x = x+1;
            m.y = y;
            candidates.push_back(m);
        }

        if((x-1)>=0 && V[x-1][y] == false && grid[x-1][y] != 0){
            m.x = x-1;
            m.y = y;
            candidates.push_back(m);
        }

        if((y-1)>=0 && V[x][y-1] == false && grid[x][y-1] != 0){
            m.x = x;
            m.y = y-1;
            candidates.push_back(m);
        }

        if((y+1)<=grid[0].size()-1 && V[x][y+1] == false && grid[x][y+1] != 0){
            m.x = x;
            m.y = y+1;
            candidates.push_back(m);
        }

        if(candidates.size() == 0){
            max_sum = max(max_sum,sum);
            return;
        }else{
            for(int i=0;i<candidates.size();i++){
                sum += grid[candidates[i].x][candidates[i].y];
                x = candidates[i].x;
                y = candidates[i].y;
                V[x][y] = true;
                dfs(sum,x,y,V,grid,max_sum);
                V[x][y] = false;
                sum -= grid[candidates[i].x][candidates[i].y];
            }
        }
    }


    /*
    usiamo il bactracking, andando a sfruttura una matrice di booleani 
    per capire se una data cella è gia stata visitata
    la visita si ferma quando non possiamo andare da nessuna parte
    non posso visitare una cella con uno 0
    */
    int getMaximumGold(vector<vector<int>>& grid) {
        
        vector<vector<bool>> V(grid.size(),vector<bool>(grid[0].size(),false));
        int max_gold = 0;
        for(int i=0;i<grid.size();i++){
            for(int j=0;j<grid[0].size();j++){
                if(grid[i][j] != 0){
                    int sum = grid[i][j];
                    V[i][j] = true;
                    dfs(sum,i,j,V,grid,max_gold);
                    V[i][j] = false;
                }
            }
        }
        return max_gold;
    }
};
186000