Skip to content

Instantly share code, notes, and snippets.

@rpf5573
Created March 22, 2019 04:01
Show Gist options
  • Save rpf5573/4af311d062b184d1bf665526a3a7ab32 to your computer and use it in GitHub Desktop.
Save rpf5573/4af311d062b184d1bf665526a3a7ab32 to your computer and use it in GitHub Desktop.
백준알고리즘
# from : https://www.acmicpc.net/problem/14503
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
CLEAN_SPACE = 2
def solution(table, position, direction):
noWay = False
currentDirection = direction
table[position[0]][position[1]] = CLEAN_SPACE
while not noWay:
newDirection = findDirection(table, position, currentDirection)
if newDirection is not None :
position = forward(position, newDirection)
table[position[0]][position[1]] = CLEAN_SPACE
currentDirection = newDirection
else:
position = backward(position, currentDirection)
if table[position[0]][position[1]] == 1:
noWay = True
return countCleanSpace(table)
def forward(position, direction):
if direction == NORTH:
return [position[0] - 1, position[1]]
if direction == EAST:
return [position[0], position[1] + 1]
if direction == SOUTH:
return [position[0] + 1, position[1]]
if direction == WEST:
return [position[0], position[1] - 1]
def findDirection(table, position, direction):
for _ in range(4):
if direction == NORTH:
if table[position[0]][position[1]-1] == 0:
return ((direction + 3)%4)
elif direction == EAST:
if table[position[0] - 1][position[1]] == 0:
return ((direction + 3)%4)
elif direction == SOUTH:
if table[position[0]][position[1] + 1] == 0:
return ((direction + 3)%4)
elif direction == WEST:
if table[position[0] + 1][position[1]] == 0:
return ((direction + 3)%4)
direction = ((direction+3)%4)
return None
def backward(position, direction):
if direction == NORTH:
return [position[0]+1, position[1]]
if direction == EAST:
return [position[0], position[1]-1]
if direction == SOUTH:
return [position[0]-1, position[1]]
if direction == WEST:
return [position[0], position[1]+1]
def countCleanSpace(table):
totalCount = 0
for i in range(len(table)):
totalCount = totalCount + table[i].count(CLEAN_SPACE)
return totalCount
i = 0
tableSize = list(map(int, input().split()))
positionAndDirection = list(map(int, input().split()))
position = [positionAndDirection[0], positionAndDirection[1]]
direction = positionAndDirection[2]
table = [0 for _ in range(tableSize[0])]
while i < len(table):
table[i] = list(map(int, input().split()))
i = i + 1
print(solution(table, position, direction))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment