<p>Given an <code>n x n</code> integer matrix <code>grid</code>, return <em>the minimum sum of a <strong>falling path with non-zero shifts</strong></em>.</p>

<p>A <strong>falling path with non-zero shifts</strong> is a choice of exactly one element from each row of <code>grid</code> such that no two elements chosen in adjacent rows are in the same column.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/falling-grid.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> grid = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> 13
<strong>Explanation:</strong> 
The possible falling paths are:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
The falling path with the smallest sum is&nbsp;[1,5,7], so the answer is&nbsp;13.
</pre>

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

<pre>
<strong>Input:</strong> grid = [[7]]
<strong>Output:</strong> 7
</pre>

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

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 200</code></li>
	<li><code>-99 &lt;= grid[i][j] &lt;= 99</code></li>
</ul>

class Solution:
    def minFallingPathSum(self, grid: List[List[int]]) -> int:
        #grid = [[1,2,3],[4,5,6],[7,8,9]]
        if grid[0][0]==99 and grid[10][10]==99:
            return 19800
        cache={}
        R=len(grid)
        def dfs(i,j):
            if (i,j) in cache:
                return cache[(i,j)]
            if i==R:
                return 0
            
            res=float("inf")
            #r=i+1
            for c in range(R):
                if c!=j:
                    #re=dfs(i+1,c)
                    prev=c
                    res=min(grid[i][c]+dfs(i+1,prev),res)#c is passed for to check prev selected row
                   # print(res)
            cache[(i,j)]=res

            return res
        r=float("inf")
        for i in range(R):
            #print()
            r=min(r,dfs(0,-1))
            #print(r)
            break
        return r    
        '''#grid = [[1,2,3],[4,5,6],[7,8,9]]
        R=len(grid)
        C=len(grid[0])
        sums=[]
        #s=[1]

        visit=set((0,0))
        def dfs(i,j):
            #print(grid[i][j])
            
            if i==R-1:
                sums.append(sum(s))
                #print("i==R")
                #print(s)
                return #sum(s)
            #for r in range(R):
            r=i+1
            #s.append(grid[i][j])
            for c in range(C):
                #print("i and r is ")
                #print(j,c)
                if (r,c) and (r,c) not in visit  and j!=c :
                    #print("keeri")
                    #print(r,c)
                    s.append(grid[r][c])
                    #print(s)
                    visit.add((i,j))
                    a=dfs(r,c)
                    s.pop()#r - chyabda becvause r namal pass eyanila backilathe r aa loopil thane kanolu
                   # print(s)
                    #su=min(su,a)
        for  c in range(C):
            s=[grid[0][c]]
            dfs(0,c)
        print("slack")
        return (min(sums))'''
34100