Editorial for Moving Ball


Author:kahootist

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

Code 1
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)

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.