|
| 1 | +# https://maang.in/problems/One-Piece-902?resourceUrl=cs214-cp873-pl3343-rs902&returnUrl=%5B%22%2Fcourses%2FGraph-Level-1-214%3Ftab%3Dchapters%22%5D |
| 2 | + |
| 3 | +''' |
| 4 | +Description |
| 5 | +Monkey D. Luffy, on his journey of becoming the "King of Pirates" and to conquer the "One Piece", wants to travel across the Grand Line. Grand Line is a mysterious sea, and is in the shape of a N×M grid S with every cell denoting the wind direction. The sign of |
| 6 | +S[i][j] can be: 1 which means wind in the cell flows to the right (i.e. from |
| 7 | +S[i][j] to |
| 8 | +S[i][j+1]), 2 which means wind in the cell flows to the left (i.e. from |
| 9 | +S[i][j] to |
| 10 | +S[i][j−1]), |
| 11 | +3 which means wind in the cell flows downwards (i.e. from |
| 12 | +S[i][j] to |
| 13 | +S[i+1][j]), or |
| 14 | +4 which means wind in the cell flows upwards (i.e. from |
| 15 | +S[i][j] to |
| 16 | +S[i−1][j]). Notice that there could be some signs on the cells of the grid |
| 17 | +S that point outside the Grand Line sea grid. |
| 18 | +
|
| 19 | +Merry, Luffy's ship, can only sail along the wind direction and cannot go outside the Grand Line sea grid |
| 20 | +S at any point. Luffy can modify the wind's direction on a cell with a |
| 21 | +cost=1 (he can modify the sign on a cell one time only). |
| 22 | +
|
| 23 | +Find the minimum cost to make Luffy's voyage from the top-left corner of the Grand Line, i.e., |
| 24 | +S[1][1], to its bottom-right corner, i.e., |
| 25 | +S[N][M], possible. |
| 26 | +
|
| 27 | +Input Format |
| 28 | +Input is given from Standard Input in the following format: |
| 29 | +S 1,1 S 1,2 |
| 30 | + …S |
| 31 | +1,M |
| 32 | + |
| 33 | +Output Format |
| 34 | +Print the minimum total cost required to make a path from |
| 35 | +S[1][1] to |
| 36 | +S[N][M]. |
| 37 | +
|
| 38 | +Constraints |
| 39 | +2≤N,M≤1000. |
| 40 | +N and M are integers. |
| 41 | +
|
| 42 | +Sample Input 1 |
| 43 | +4 4 |
| 44 | +1 1 1 1 |
| 45 | +2 2 2 2 |
| 46 | +1 1 1 1 |
| 47 | +2 2 2 2 |
| 48 | +Sample Output 1 |
| 49 | +3 |
| 50 | +Sample Input 2 |
| 51 | +3 3 |
| 52 | +1 1 3 |
| 53 | +3 2 2 |
| 54 | +1 1 4 |
| 55 | +Sample Output 2 |
| 56 | +0 |
| 57 | +''' |
| 58 | +# Write your code here |
| 59 | +import sys |
| 60 | +from collections import deque |
| 61 | + |
| 62 | +input=sys.stdin.readline |
| 63 | + |
| 64 | +INF=10**18 |
| 65 | + |
| 66 | +dirs=[ |
| 67 | + (0,1), |
| 68 | + (0,-1), |
| 69 | + (1,0), |
| 70 | + (-1,0) |
| 71 | +] |
| 72 | + |
| 73 | +def solve(): |
| 74 | + n,m=list(map(int,input().split())) |
| 75 | + grid=[] |
| 76 | + for _ in range(n): |
| 77 | + grid.append(list(map(int,input().split()))) |
| 78 | + |
| 79 | + dist=[[INF]*m for _ in range(n)] |
| 80 | + dist[0][0]=0 |
| 81 | + |
| 82 | + q=deque([(0,0)]) |
| 83 | + while q: |
| 84 | + x,y=q.popleft() |
| 85 | + for k in range(4): |
| 86 | + nx,ny=x+dirs[k][0],y+dirs[k][1] |
| 87 | + if 0<=nx<n and 0<=ny<m: |
| 88 | + cost=0 if grid[x][y]==k+1 else 1 |
| 89 | + if dist[x][y]+cost<dist[nx][ny]: |
| 90 | + dist[nx][ny]=cost+dist[x][y] |
| 91 | + if cost==0: |
| 92 | + q.appendleft((nx,ny)) |
| 93 | + else: |
| 94 | + q.append((nx,ny)) |
| 95 | + print(dist[n-1][m-1]) |
| 96 | + |
| 97 | +if __name__=="__main__": |
| 98 | + solve() |
| 99 | + |
| 100 | + |
| 101 | +# Time Complexity per test case: O(N×M) because each cell is processed at most a constant number of times and there are 4 edges per cell. |
| 102 | + |
| 103 | +# Space Complexity per test case: O(N×M) for the distance matrix and the deque. |
0 commit comments