<p>Given an <code>m x n</code> grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of <code>grid[i][j]</code> can be:</p>

<ul>
	<li><code>1</code> which means go to the cell to the right. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j + 1]</code>)</li>
	<li><code>2</code> which means go to the cell to the left. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j - 1]</code>)</li>
	<li><code>3</code> which means go to the lower cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i + 1][j]</code>)</li>
	<li><code>4</code> which means go to the upper cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i - 1][j]</code>)</li>
</ul>

<p>Notice that there could be some signs on the cells of the grid that point outside the grid.</p>

<p>You will initially start at the upper left cell <code>(0, 0)</code>. A valid path in the grid is a path that starts from the upper left cell <code>(0, 0)</code> and ends at the bottom-right cell <code>(m - 1, n - 1)</code> following the signs on the grid. The valid path does not have to be the shortest.</p>

<p>You can modify the sign on a cell with <code>cost = 1</code>. You can modify the sign on a cell <strong>one time only</strong>.</p>

<p>Return <em>the minimum cost to make the grid have at least one valid path</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid1.png" style="width: 400px; height: 390px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You will start at point (0, 0).
The path to (3, 3) is as follows. (0, 0) --&gt; (0, 1) --&gt; (0, 2) --&gt; (0, 3) change the arrow to down with cost = 1 --&gt; (1, 3) --&gt; (1, 2) --&gt; (1, 1) --&gt; (1, 0) change the arrow to down with cost = 1 --&gt; (2, 0) --&gt; (2, 1) --&gt; (2, 2) --&gt; (2, 3) change the arrow to down with cost = 1 --&gt; (3, 3)
The total cost = 3.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid2.png" style="width: 350px; height: 341px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,3],[3,2,2],[1,1,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> You can follow the path from (0, 0) to (2, 2).
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid3.png" style="width: 200px; height: 192px;" />
<pre>
<strong>Input:</strong> grid = [[1,2],[4,3]]
<strong>Output:</strong> 1
</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;= 100</code></li>
	<li><code>1 &lt;= grid[i][j] &lt;= 4</code></li>
</ul>

class Solution {
public:
    bool chk(int& i, int& j, vector<vector<int>>& grid)
    {
        if(i<0 || i>=grid.size() || j<0 || j>=grid[0].size()){return false;}
        return true;
    }

    int get_val(int& i, int& j)
    {
        return (((i+j)*(i+j+1))/2)+j;
    }

    int get_direction(int& val)
    {
        if(val==1){return 2;}
        else if(val==2){return 3;}
        else if(val==3){return 0;}
        return 1;
    }

    int minCost(vector<vector<int>>& grid) 
    {
        int sz = grid.size()*grid[0].size();
        unordered_map<int,int> map;
        int x = 0;
        for(int i=0; i<grid.size(); i++)
        {
            for(int j=0; j<grid[i].size(); j++)
            {
                map[get_val(i,j)] = x;
                x++;
            }
        }

        vector<vector<int>> move = {{1,0},{-1,0},{0,1},{0,-1}};
        vector<vector<pair<int,int>>> adjl(sz, vector<pair<int,int>>());
        for(int i=0; i<grid.size(); i++)
        {
            for(int j=0; j<grid[i].size(); j++)
            {
                int dir = get_direction(grid[i][j]);
                for(int k=0; k<move.size(); k++)
                {
                    int rw = i + move[k][0];
                    int col = j + move[k][1];
                    if(chk(rw, col, grid))
                    {
                        if(k==dir)
                        {
                            adjl[map.find(get_val(i,j))->second].push_back(make_pair(map.find(get_val(rw,col))->second,0));
                        }
                        else
                        {
                            adjl[map.find(get_val(i,j))->second].push_back(make_pair(map.find(get_val(rw,col))->second,1));
                        }
                    }
                }
            }
        }

        vector<int> dist(sz,INT_MAX);
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
        int l=0, m=0;
        int source = map.find(get_val(l,m))->second;
        dist[source] = 0;
        pq.push(make_pair(0,source));
        while(!pq.empty())
        {
            int u = pq.top().second;
            pq.pop();
            for(int i=0; i<adjl[u].size(); i++)
            {
                int neigh = adjl[u][i].first;
                int weigh = adjl[u][i].second;
                if(dist[neigh]>dist[u]+weigh)
                {
                    dist[neigh] = dist[u] + weigh;
                    pq.push(make_pair(dist[neigh],neigh));
                }
            }
        }

        l = grid.size()-1;
        m = grid[0].size()-1;
        return dist[map.find(get_val(l,m))->second];
    }
};
45379