programmers | 공원 산책
global width, height, x, y, axis, direction, count
def sign(num):
if num >= 0:
return 1
return -1
def hasRock(park):
global x, y, axis, count
s = sign(count)
abscount = abs(count)
if axis == 'x':
for d in range(abscount + 1):
if park[y][x + s * d] == 'X':
return True
elif axis == 'y':
for d in range(abscount + 1):
if park[y + s * d][x] == 'X':
return True
return False
def Move(park, route):
global width, height, x, y, axis, direction, count
if axis == 'x':
dest = x + count
if dest < 0 or width <= dest:
return False
elif hasRock(park) == True:
return False
x += count
elif axis == 'y':
dest = y + count
if dest < 0 or height <= dest:
return False
elif hasRock(park) == True:
return False
y += count
return True
def solution(park, routes):
global width, height, x, y, axis, direction, count
width, height = len(park[0]), len(park)
for i in range(height):
for j in range(width):
if park[i][j] == 'S':
y, x = i, j
break
for route in routes:
direction = route[0:1]
count = int(route[1:])
if direction == 'W' or direction == 'N':
count *= -1
if direction == 'E' or direction == 'W':
axis = 'x'
else:
axis = 'y'
Move(park, route)
return [y, x]
- 다른 사람 풀이를 보니..
- range에 +1 해주는거 찾는데 오래걸림
- 나머지는 기계적으로 푼듯...