본문 바로가기

코딩게임

[codingame 번역] Blunder - episode 1

 

Solutions for the exercise "Blunder - Episode 1"

Learn from others by looking at their solutions for this programming puzzle.

www.codingame.com

목표(GOAL)

"블런다" 는 우울한 로봇이며 파티와 마시기로 우을함을 치유 합니다.
그의 방탕함으로부터 그를 저장하기위해 그를 만든사람은 
제어 시스템을 기초적인것으로 리프로그래밍 하였습니다.
그치만 불행이도, 그는 그의 유머감각을 잃었고 그의 친구들은 그를 거부 했습니다.

이윽고 '블런다'는 혼자가 되었고 자살부스에서 끝장을 보려고 '퓨처라마' 길거리를 배회하고 있습니다.

그를 저장하기 위하여 그의 관계자는 당신에게 미션을 주었습니다.
'블런다'의 경로를 예측하는 프로그램을 작성하세요.
그렇게 하기 위해, 당신은 '블런다'에게 프로그램된 새로운 도시지도에 대한 지능로직을 제공받는다.

Self kill booth



규칙(Rules)

아홉가지 '블런다'의 새 규칙 시스템
1. '블런다'는 '@' 심볼 에서 출발하고 방향은 남쪽을 향한다.
2. '$' 인 자살부스에 다다르면 '블런다'는 그의 여정을 끝내고 죽는다.
3. '블런다가' 만나게 될 장애물은 '#' 또는 'X' 다
4. '블런다'는 장애물을 만나면 다음의 우선순위에 의해 방향을 변경한다.
 남쪽, 동쪽, 북쪽, 서쪽 순이다.
 예를 들어, 남쪽으로 진행하여 더이상 진행하지 못할경우 동쪽으로 진행하고, 
 그것도 안되면 북쪽, 또 안되면 서쪽으로 진행한다.
5. 길을 가다가 블런다는 수정 표식을 만나면 즉각적으로 경로를 변경합니다.
 S: 남쪽, E: 동쪽, W: 서쪽, N: 북쪽
6. 'I'를 만나면 우선순위가 거꾸로 변경됩니다. (서, 북 동, 남) 그리고 다시 만나면 초기화 됩니다.
7. '블런다'는 또한 'B' (맥주) 를 만나면 힘을 받아서 파괴모드가 활성화 되어 'X' 인 장애물을 파괴하고 방향을 그대로 유지한 채 진행합니다.
 그 후 다시 'B'를 만나면 파괴모드는 해제됩니다.
8. 2개 중의 하나의 'T'를 만나면 다른 한쪽의 'T'로 텔레포트 합니다. 이 때, 방향과 파괴모드는 유지됩니다.
9. 마지막으로 ' ' (space) 는 빈구역을 의미 합니다. 

당신의 프로그램은 제공된 맵에 대한 블런다의 이동순서를 출력 해야 합니다.

맵은 라인 L 과 컬럼 C 로 구분되어 있습니다.
맵의 윤각은 항상 파괴 할 수 없는 '#'로 표시됩니다.
그리고 '@'는 항상 시작위치이며, '$' 는 자살 부스 입니다.

만약 자살 부스에 도달하지 못하고 계속 반복한다면 'LOOP'를 출력하세요

 


<예시>

######
#@E $#
#   N#
#X   #
######


이 맵에서는 , 블런다는 하기의 행동 순서를 따릅니다.
SOUTH (최초 방향)
EAST (이유 : 'X'에 의해 남쪽 이동 불가)
NORTH ('N'에 의해 북쪽으로 변경)
EAST ('E'에 의해 동쪽으로 변경)
EAST (방향 유지 그리고 자살부스 도착, 그리고 종착)

 


Game input

라인 1 : 라인수 L 과 컬럼 수 C (사이 공백)
다음 라인 : 각 라인에 해당하는 맵 캐릭터.  #, X, @, $, S, E, N, W, B, I, T 그리고 공백

 


Output

만약 'S'에 도달 할 수 있다면 각각의 이동방향을 라인마다 출력.
도달 할 수 없으면 LOOP 를 출력.

//제약 조건
4 ≤ C ≤ 100
4 ≤ L ≤ 100

 

더보기

 The Goal

Blunder is a depressed robot who heals his depression by partying and drinking alcohol. To save him from a life of debauchery, his creators have reprogrammed the control system with a more rudimentary intelligence. Unfortunately, he has lost his sense of humor and his former friends have now rejected him.

Blunder is now all alone and is wandering through the streets with the intention of ending it all in a suicide booth.

To intercept him and save him from almost certain death, the authorities have given you a mission: write a program that will make it possible to foresee the path that Blunder follows. To do so, you are given the logic for the new intelligence with which Blunder has been programmed as well as a map of the city.

 Rules

The 9 rules of the new Blunder system:

  1. Blunder starts from the place indicated by the @ symbol on the map and heads SOUTH.
  2. Blunder finishes his journey and dies when he reaches the suicide booth marked $.
  3. Obstacles that Blunder may encounter are represented by # or X.
  4. When Blunder encounters an obstacle, he changes direction using the following priorities: SOUTH, EAST, NORTH and WEST. So he first tries to go SOUTH, if he cannot, then he will go EAST, if he still cannot, then he will go NORTH, and finally if he still cannot, then he will go WEST.
  5. Along the way, Blunder may come across path modifiers that will instantaneously make him change direction. The S modifier will make him turn SOUTH from then on, E, to the EAST, N to the NORTH and W to the WEST.
  6. The circuit inverters (I on map) produce a magnetic field which will reverse the direction priorities that Blunder should choose when encountering an obstacle. Priorities will become WEST, NORTH, EAST, SOUTH. If Blunder returns to an inverter I, then priorities are reset to their original state (SOUTH, EAST, NORTH, WEST).
  7. Blunder can also find a few beers along his path (B on the map) that will give him strength and put him in “Breaker” mode. Breaker mode allows Blunder to destroy and automatically pass through the obstacles represented by the character X (only the obstacles X). When an obstacle is destroyed, it remains so permanently and Blunder maintains his course of direction. If Blunder is in Breaker mode and passes over a beer again, then he immediately goes out of Breaker mode. The beers remain in place after Blunder has passed.
  8. 2 teleporters T may be present in the city. If Blunder passes over a teleporter, then he is automatically teleported to the position of the other teleporter and he retains his direction and Breaker mode properties.
  9. Finally, the space characters are blank areas on the map (no special behavior other than those specified above).
Your program must display the sequence of moves taken by Blunder according to the map provided as input.

The map is divided into lines (L) and columns (C). The contours of the map are always unbreakable # obstacles. The map always has a starting point @ and a suicide booth $.

If Blunder cannot reach the suicide booth because he is indefinitely looping, then your program must only display LOOP.

 Example

Let the map below:

######
#@E $#
#   N#
#X   #
######

In this example, Blunder will follow this sequence of moves:

  • SOUTH (initial direction)
  • EAST (because of the obstacle X)
  • NORTH (change of direction caused by N)
  • EAST (change of direction caused by E)
  • EAST (current direction, until end point $)

 Game Input

Input

Line 1: the number of lines L and columns C on the map, separated by a space.

The following L lines: a line of the length C representing a line on the map. A line can contain the characters #, X, @, $, S, E, N, W, B, I, T and space character.

Output
  • If Blunder can reach $, then display the sequence of moves he has taken. One move per line: SOUTH for the South, EAST for the East, NORTH for the North and WEST for the west.
  • If Blunder cannot reach $, then only display LOOP.
Constraints
4 ≤ C ≤ 100
4 ≤ L ≤ 100
Example
Input
10 10
##########
#        #
#  S   W #
#        #
#  $     #
#        #
#@       #
#        #
#E     N #
##########
Output
SOUTH
SOUTH
EAST
EAST
EAST
EAST
EAST
EAST
NORTH
NORTH
NORTH
NORTH
NORTH
NORTH
WEST
WEST
WEST
WEST
SOUTH
SOUTH

 

<역자 후기> (블로그 주인)

저는 아내를 저장하지 못했지만,, 그를 저장하기 위하여 코드를 작성 했습니다.

 

더보기
import sys
import math

 

class vec(tuple):
    def __add__(self, other):
        return vec(x + y for x, y in zip(self,other))

 

l, c = [int(i) for i in input().split()]
map = [None]*l
tele = set()
for i,line in enumerate(sys.stdin):
    col = line.find('@')
    if ~col: pt = vec((col,i))
    t1 = line.find('T')
    t2 = line.rfind('T')
    if ~t1: tele |= {(t1,i)} | {(t2,i)}
    map[i] = list(line)

 

ordr = {"CUR":(0,1), "SOUTH":(0,1), "EAST":(1,0), "NORTH":(0,-1), "WEST":(-1,0)}
inv, pwr, dr, vis, path = [1, False, 'SOUTH', {0}, []]
destroy = 0

 

while map[pt[1]][pt[0]] != '$':
    k = tuple(pt) + (dr, inv, pwr, destroy)
    if k in vis: print("LOOP");exit(0)
    vis.add(k)
    *lk,=ordr.keys()
    for v in range(5):
        nd = lk[v*inv]
        nx = pt+ordr[nd]
        c = map[nx[1]][nx[0]]
        if c != '#' and (c!='X' or pwr): break

 

    if nd!="CUR": dr=nd
    path.append(dr)

 

    if c=='I': inv = -inv
    elif c=='B': pwr = not pwr
    elif c=='X':
        map[nx[1]][nx[0]] = ' '
        destroy += 1
    elif c=='T': nx = vec((*(tele-{pt}),)[0])
    elif c in 'SENW': dr = lk[' SENW'.find(c)]

 

    ordr["CUR"] = ordr[dr]
    pt = nx
   
for line in path: print(line)

'코딩게임' 카테고리의 다른 글

[codingame 번역] TAN Network  (0) 2021.12.24