1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| n, m = map(int, input().split()) a, b, d = map(int, input().split()) matrix = []
ngo = [(0, -1), (1, 0), (0, 1), (-1, 0)]
nback = [(0, 1), (-1, 0), (0, -1), (1, 0)]
for i in range(m): matrix.append(list(map(int, input().split())))
count = 1 turned = 0
matrix[a][b] = 2
while True: if turned == 4: turned = 0 by, bx = a + nback[d][0], b + nback[d][1] if matrix[by][bx] == 1: break else: a = by b = bx turned = 0 count += 1 else: ny, nx = a + ngo[d][0], b + ngo[d][1] if matrix[ny][nx] == 1 or matrix[ny][nx] == 2: turned += 1 d = (d + 1) % 4 continue else: a = ny b = nx matrix[ny][nx] = 2 turned = 0 d = (d + 1) % 4 count += 1
print(count)
|