언어/python
정렬
파아랑새
2018. 4. 12. 09:01
def main():
dal_size = int(input(": "))
check_list = list()
check_list.extend([['empty']*dal_size for _ in range(0, dal_size)])
# 방향키
direction_list = ['right', 'down', 'left', 'up']
temp_index = 0
"""
right => col +=1 , 고려사항 col 사이즈
down => row +=1 , 고려사항 row 사이즈
left => col -=1
up => row -=1
"""
# 임시 인덱스
index_list = [0,-1]
# 반복문 이동
for i in range(1, dal_size**2 + 1):
if direction_list[temp_index] == 'right':
"""이동전에 col 사이즈를 벗어나는지 확인"""
# case -1 ) 벗어난다면
if index_list[1] + 1 == dal_size:
# case -1-1) down
if check_list[index_list[0]+1][index_list[1]] == "empty":
index_list[0] = index_list[0] + 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
# case -2 ) 벗어나지 않는다면
else:
# col 이동 +1
"""이동한 후 해당 위치에 값이 있을 경우"""
if check_list[index_list[0]][index_list[1] + 1] != "empty":
""" down 이동 """
if check_list[index_list[0]+1][index_list[1]] == "empty":
index_list[0] = index_list[0] + 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
else: # == empty
index_list[1] = index_list[1] + 1
check_list[index_list[0]][index_list[1]] = i
elif direction_list[temp_index] == 'down':
"""이동전에 row 사이즈를 벗어나는지 확인"""
# case -1 ) 벗어난다면
if index_list[0] + 1 == dal_size:
# case -1-1) left
if check_list[index_list[0]][index_list[1]-1] == "empty":
index_list[1] = index_list[1] - 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
# case -2 ) 벗어나지 않는다면
else:
# col 이동 +1
"""이동한 후 해당 위치에 값이 있을 경우"""
if check_list[index_list[0]+1][index_list[1]] != "empty":
""" left 이동 """
if check_list[index_list[0]][index_list[1]-1] == "empty":
index_list[1] = index_list[1] - 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
else: # == empty
index_list[0] = index_list[0] + 1
check_list[index_list[0]][index_list[1]] = i
elif direction_list[temp_index] == 'left':
"""이동전에 col 사이즈를 벗어나는지 확인"""
# case -1 ) 벗어난다면
if index_list[1] - 1 == -1:
# case -1-1) up
if check_list[index_list[0]-1][index_list[1]] == "empty":
index_list[0] = index_list[0] - 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
# case -2 ) 벗어나지 않는다면
else:
# col 이동 +1
"""이동한 후 해당 위치에 값이 있을 경우"""
if check_list[index_list[0]][index_list[1] - 1] != "empty":
""" up 이동 """
if check_list[index_list[0]-1][index_list[1]+1] == "empty":
index_list[0] = index_list[0] - 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
else: # == empty
index_list[1] = index_list[1] - 1
check_list[index_list[0]][index_list[1]] = i
else:
""" up """
"""이동전에 row 사이즈를 벗어나는지 확인"""
# case -1 ) 벗어난다면
if index_list[0] - 1 == -1:
# case -1-1) right
if check_list[index_list[0]][index_list[1]+1] == "empty":
index_list[1] = index_list[1] + 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
# case -2 ) 벗어나지 않는다면
else:
# col 이동 +1
"""이동한 후 해당 위치에 값이 있을 경우"""
if check_list[index_list[0]-1][index_list[1]] != "empty":
""" right 이동 """
if check_list[index_list[0]][index_list[1]+1] == "empty":
index_list[1] = index_list[1] + 1
check_list[index_list[0]][index_list[1]] = i
# 방향 이동 유도
temp_index = (temp_index + 1)%len(direction_list)
else: # == empty
index_list[0] = index_list[0] - 1
check_list[index_list[0]][index_list[1]] = i
for i in range(0, dal_size):
for j in range(0, dal_size):
print ("{0:2d}".format(check_list[i][j]), end=' ')
print()
if __name__ =="__main__":
main()