Here is the program I'm trying to write:
The user gives the properties of some domino pieces, some of which are vertical, and some are horizontal, and they are in n rows and m columns. Suppose we are looking at them from above: a horizontal domino can fall either to the right or to the left, and can only make other horizontal dominoes to fall. and a vertical domino can fall to the up or down and can only cause other vertical dominoes to fall.
first the user will give us the number of rows and columns as 'n m', and in the following n lines of input, the user will give lines each made of m characters, like for example m=5: ||-|- The '|' represents a horizontal and the '-' represents a vertical domino.
Now the program is supposed to determine the minimum number of dominoes which we have to push to make them all to fall.
this is the example presented in the question:
input:
3 3
|||
||-
||-
output:
4
We push the first dominoes in each row and the 2nd one in the third column (from left to right)
So here is my code:
dim = input()
n, m = [int(i) for i in dim.split(' ')]
min_d = 0
vertical = []
for q in range(0, m):
vertical.append(n)
for j in range(0, n):
line = list(input())
# horizontal:
if line[0] == '|':
min_d += 1
for l in range(1, m):
if line[l] == '|':
if line[l-1] == '-':
min_d += 1
# vertical:
if j == 0:
for k in range(0, m):
if line[k] == '-':
min_d += 1
vertical[k] = 0
if j > 0:
for p in range(0, m):
if line[p] == '-':
if vertical[p] != j-1:
min_d += 1
vertical[p] = j
print(min_d)
and it works fine for the above example and some other examples I made up and calculated manually. But when I submit this on the site, I get "WRONG ANSWER!" for all the tests! What is wrong with this?
WRONG ANSWER!? - Hearnerfor q in range(0, m): vertical.append(n)this will just give you[3,3,3]- Mitchverticalarray flags the identity of the cell of the respective column in the preceding line of the input matrix. The approach is feasible since one only needs to track a change of symbols when sweeping along a column or a line.verticalentries hold the index of the previous line and are updated whenever a h-aligned domino is encountered. If it doesn't, theverticalentry 'lags behind' henceforth, triggering another increment at the next '-'.verticalis initialised with the number of lines, a value that can never be a legal identity marker. - collapsar