Editorial for Moving Ball
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Editorial
Notice how only the edges of the grid matter when determining whether a ball will go out of bounds or not. Therefore, we only have to check all cells at an edge and determine whether it'll lead the balls out of bounds or not.
Implementation
n, m = map(int, input())
grid = [input().split() for _ in range(n)]
changes = 0
for j in range(m):
if grid[0][j] == 'U':
changes += 1
if grid[n-1][j] == 'D':
changes += 1
for i in range(n):
if grid[i][0] == 'L':
changes += 1
if grid[i][m-1] == 'R':
changes += 1
print(changes)
Comments