Snake

면접 대비

시간 제한2초메모리 제한1024 MB

요약
번호가 붙은 뱀과 사과 하나가 있는 격자에서 뱀의 머리가 사과에 도달할 수 있는지 판정한다.
난이도

보통10점 중 7점

유형
BFS, 시뮬레이션, 그래프
정답자
아직 제출이 없습니다

문제

Google’s version of Snake

Snake is a video game classic, preserved at the Museum of Modern Art (MoMA) and listed as one of the “Top 100 Video Games” of all time. The goal of the game is to move a snake’s head to an apple. Once the snake reaches the apple, it eats it and grows in length. A new apple is placed, which the now grown snake must then eat.

The game is played on a grid, and every segment of the snake’s body occupies one cell. The snake’s head can turn in three directions, but it cannot go backwards. The body follows the head. The head may not collide with the body or exit the grid. Since the entire snake moves at the same time, the head is allowed to enter the cell that the tail is vacating.

Playing the game requires quickness and foresight. It’s all too easy to take turns that put the snake head in a position where it’s doomed to hit the wall or its body before reaching the apple, especially as the snake grows longer.

You’re being asked to write a program that can determine whether the snake’s head can reach the apple from a given position, or not and the snake is doomed to die.

입력

The first line of output contains two integers rr and cc (1≤r,c≤101≤r,c≤10, r⋅c≥2r \cdot c≥2), where the grid has rr rows and cc columns.

Each of the next rr lines contains a string of length exactly cc characters from the set

{‘.’,‘0’, … … ,‘9’,‘a’, … … ,‘f’,‘A’}

where ‘.’ represents an open cell in the grid, the hexadecimal digits ‘0’, … … ,‘9’ and ‘a’, … … ,‘f’ represent the snake, and ‘A’ represents the apple. The snake may be anywhere from one to sixteen characters long, with ‘0’ as its head, followed by the other hexadecimal digits in strict order (‘1’ follows ‘0’, ‘2’ follows ‘1’, etc., with no skipping digits.). It is guaranteed that there is at most one of each digit, each digit (except ‘0’) is adjacent to the immediately previous digit, and that there is exactly one apple in the grid.

출력

Output a single integer, which is 11 if the snake can reach the apple, and 00 if it cannot and is doomed to die.

예제4

  1. 예제 1

    입력
    5 8
    ......01
    ....98.2
    ...A.7.3
    .....654
    ........
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5 4
    ...A
    ....
    6789
    5432
    ..01
    
    예상 출력
    0
    
  3. 예제 3

    입력
    5 5
    ....A
    .....
    678..
    54321
    ....0
    
    예상 출력
    1
    
  4. 예제 4

    입력
    4 4
    567A
    4389
    12ba
    0dc.
    
    예상 출력
    1