格子里的整數(shù)

Description

Given a square grid of size n, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which total cost incurred is minimum.

Note : It is assumed that negative cost cycles do not exist in input matrix.

Input

The first line of input will contain number of test cases T. Then T test cases follow . Each test case contains 2 lines. The first line of each test case contains an integer n denoting the size of the grid. Next line of each test contains a single line containing N*N space separated integers depecting cost of respective cell from (0,0) to (n,n).

Constraints:1<=T<=50,1<= n<= 50

Output

For each test case output a single integer depecting the minimum cost to reach the destination

Sample Input 1
2
5
31 100 65 12 18 10 13 47 157 6 100 113 174 11 33 88 124 41 20 140 99 32 111 41 20
2
42 93 7 14
Sample Output1
327
63

思路

這道題沒(méi)有限制只能向下和向右走,所以也可以往回走,就不能用動(dòng)態(tài)規(guī)劃,遞進(jìn)的方式求解。

考慮用深度優(yōu)先的方法,

首先初始化一個(gè)距離數(shù)組mindis,大小和格子相同,代表從左上角起點(diǎn),到每個(gè)位置花費(fèi)的最小值。

從左上角(0,0)開(kāi)始,深度優(yōu)先訪問(wèn)其相鄰的點(diǎn),

假設(shè)當(dāng)前位置是(x,y),檢查(x,y)4個(gè)方向的點(diǎn)(x+1, y), (x-1, y), (x, y+1), (x, y-1)

如果在格子內(nèi),且這個(gè)位置的距離misdis[i][j]大于mindis[x][y] + grid[x][y], 則更新misdis[i][j], 并繼續(xù)深度遍歷這個(gè)位置。

如圖所示,cur表示到達(dá)(x, y)的花費(fèi),如果

  1. 檢查(x+1, y), cur + grid[x+1][y]表示到達(dá)右邊格子的花費(fèi),如果這個(gè)值小于mindis[x+1][y]的話,就更新mindis[x+1][y],并深度遍歷(x+1, y) -> dfs(x+1, y, cur+grid[x+1][y])
  2. 檢查(x-1, y)...
  3. 檢查(x, y+1)...
  4. 檢查(x, y-1)...

深度遍歷的函數(shù)定義:

i表示橫左邊,j表示縱坐標(biāo),cur表示從左上角到達(dá)當(dāng)前位置的花費(fèi)。

function dfs(i, j, cur)

深度遍歷結(jié)束后,我們就可以得到到達(dá)每個(gè)位置的最小花費(fèi)了。

python
def solve(grid):
    m, n = len(grid), len(grid[0])
    mindis = [[float('inf')]*n for _ in range(m)]
    def dfs(i, j, cur):
        for x, y in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
            if 0 <= x < m and 0 <= y < n and mindis[x][y] > cur + grid[x][y]:
                mindis[x][y] = cur + grid[x][y]
                dfs(x, y, cur + grid[x][y])
    dfs(0, 0, grid[0][0])
    return mindis[-1][-1]

for _ in range(int(input())):
    n = int(input())
    nums = list(map(int, input().split()))
    grid = [nums[i:i+n] for i in range(0, len(nums), n)]
    print(solve(grid))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評(píng)論 0 2
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類(lèi): pyspark.sql...
    mpro閱讀 9,919評(píng)論 0 13
  • 如果你想我的時(shí)候就讓我知道好么?
    泥泥霸霸閱讀 132評(píng)論 0 0
  • 早安【日精進(jìn)第80天/3650用生命影響生命!如何過(guò)一天就如何過(guò)一生! 沒(méi)有反思的人生不值得過(guò)?。。ê笔《魇┦校?..
    好心情_(kāi)d8eb閱讀 156評(píng)論 1 0

友情鏈接更多精彩內(nèi)容