def print_grid(m, n): if m == 1 and n == 1: print("\tS/D") else: for i in range(m): for j in range(n): if i == 0 and j == 0: print("\tS", end=" ") elif i == m-1 and j == n-1: print("D", end=" ") elif j == 0: print("\tx", end=" ") # You can change "X" to any character you want else: print("x", end=" ") print() def unique_paths(m, n): # Create an array to store the number of unique paths # for each column in the grid rolling_array = [1] * n # Iterate through rows (starting from the second row) for row in range(1, m): # Iterate through columns (starting from the second column) for col in range(1, n): # Calculate unique paths to the current cell rolling_array[col] += rolling_array[col - 1] # The value in the last column of the rolling_array represents the total number of unique paths to the destination return rolling_array[-1]# Driver code def main(): input = ( [1, 1], [2, 3], [4, 4], [2, 5], [10, 10], [1, 30] ) print("S: Source, D: Destination") print("-"*100) for i in range(len(input)): print(i + 1, ".\tm = ", input[i][0], ", n = ", input[i][1], sep="") print("\n\tThe grid: ") print_grid(input[i][0], input[i][1]) ans = unique_paths(input[i][0], input[i][1]) print("\n\tTotal unique paths: ", ans) print("-"*100) if __name__ == '__main__': main()