dranz's blog

By dranz, 19 months ago, In English

Hey, I have been trying to solve this problem (2090B - Pushing Balls) where a set of balls are being pushed into a grid from either left or top. I was able to deduce that as long as the all the balls have a complete line of balls either to its left or top the state should be possible by ball pushing. I implemented this solution but it gave a WA.

def solve(arr, n, m):
    for i in range(1, n):
        for j in range(1, m):
            if arr[i][j] == '1':
                if arr[i][j-1]=='0' and arr[i-1][j]=='0':
                    return "NO"
    return "YES"
t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    arr = []
    for i in range(n):
        arr.append(list(input()))
    print(solve(arr,n,m))

The editorial also gives a similar solution and I believe the problem is with my implementation. Could someone please suggest what might be going wrong?

Thanks

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?