Ninja has put on some extra weight during lockdown. He has decided to join a gym to shed this extra weight off. He joined the Matrix Gym, where the trainer has planned an elaborate exercise for him. It is upto Ninja to decide how many calories he wants to burn during his workout, whereas the trainer designs the exercise. The basic idea of each day remains the same. Ninja has to perform his exercise by crossing his Matrix Gym, which is a 2-D Grid consisting of hurdle cells (represented by “ # ”) and clear cells (represented by “ . “ ). He has to avoid the hurdles, and by entering each clear cell he burns 5 calories. He begins from the top-left corner of the grid. It is given that starting position has no hurdle. He has to reach the bottom-right cell which is also hurdle free. You have to find out if a path exists through which Ninja can burn his decided amount of calories. Ninja can take only one step at a time. Also, when Ninja is standing at (i, j), he can move to (i, j+1) and (i+1, j) only (right and bottom cells). If such a path is possible, print “Yes”, otherwise print “No”. If more than one path is present in the gym through which the calories can be burnt, find the path which burns the maximum calories, and print the extra amount of calories burnt by Ninja.
Input Format:
Constraints:
Output Format:
Sample Input 1:
Sample Output 1:
Explanation:
Sample Input 2:
Sample Output 2:
Explanation:
Sample Input 3:
Sample Output 3:
Explanation:
from sys import stdin
2
3
# Main
4
5
n = int(stdin.readline().strip())
6
# String array
7
arr = [x for x in input().split()]
8
calories = int(stdin.readline().strip())
9
INT_MIN = -2**31
10
11
cols = len(arr[0])
12
dp = [[INT_MIN for j in range(cols + 1)] for i in range(n + 1)]
13
14
for i in range(n):
15
for j in range(cols):
16
if arr[i][j] == '.' :
17
dp[i][j] = 0
18
19
20
for i in range(n - 1, -1, -1):
21
for j in range(cols - 1, -1, -1):
22
if(i == n-1 and j == cols-1):
23
continue
24
elif(arr[i][j] == '.'):
25
dp[i][j] = max(dp[i+1][j], dp[i][j+1]) + 5
26
27
28
if(dp[0][0] < calories):
29
print("No")
30
else:
31
if dp[0][0] == calories:
32
print("Yes")
33
else:
34
print("Yes", dp[0][0] - calories)
35
Post a Comment