|
| 1 | +##문제 열심히 읽기 |
| 2 | + |
| 3 | +''' |
| 4 | +뱀은 (0,0)에 위치 길이는 1, 방향은 오른쪽 |
| 5 | +1. 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다. |
| 6 | +2. 만약 이동한 칸에 사과가 있다면, 그 칸에 있던 사과가 없어지고 꼬리는 움직이지 않는다. |
| 7 | +3. 만약 이동한 칸에 사과가 없다면, 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다. 즉, 몸길이는 변하지 않는다. |
| 8 | +final : 벽 or 자신의 몸과 부딪히면 게임 끝 |
| 9 | +
|
| 10 | +L 왼쪽, D 오른쪽 |
| 11 | +빈칸 -1, 뱀 0, 1 ..., 사과 -4 |
| 12 | +''' |
| 13 | + |
| 14 | +n=int(input()) |
| 15 | +k=int(input()) |
| 16 | +graph=[[-1]*n for _ in range(n)] |
| 17 | +move=[[0, 1], [1, 0], [0, -1], [-1, 0]] #동 남 서 북 #L면 --, D면 ++ |
| 18 | + |
| 19 | +for _ in range(k): |
| 20 | + a, b = map(int, input().split()) |
| 21 | + graph[a-1][b-1]=-4 |
| 22 | + |
| 23 | +command=[] |
| 24 | +for _ in range(int(input())): |
| 25 | + a, b = input().split() |
| 26 | + command.append([int(a), b]) |
| 27 | + |
| 28 | +snake=[[0, 0], [0, 0]] #머리, 꼬리 |
| 29 | +graph[0][0]=0 |
| 30 | +snake_dir=0 |
| 31 | +i=0 |
| 32 | + |
| 33 | +def next(tail): |
| 34 | + r, c=tail[0], tail[1] |
| 35 | + |
| 36 | + l=graph[r][c] |
| 37 | + for dr, dc in move: |
| 38 | + nr, nc=r+dr, c+dc |
| 39 | + if 0<=nr<n and 0<=nc<n and graph[nr][nc]==l+1: |
| 40 | + return [nr, nc] |
| 41 | + |
| 42 | +while 1: |
| 43 | + i+=1 |
| 44 | + head=snake[0] #r, c 순 |
| 45 | + tail=snake[1] |
| 46 | + |
| 47 | + nr, nc=head[0]+move[snake_dir][0], head[1]+move[snake_dir][1] |
| 48 | + |
| 49 | + if 0<=nr<n and 0<=nc<n and graph[nr][nc]<0: |
| 50 | + tmp=graph[nr][nc] |
| 51 | + graph[nr][nc] = i |
| 52 | + head[0], head[1] = nr, nc |
| 53 | + #사과가 없을 때 |
| 54 | + if tmp!=-4: |
| 55 | + #꼬리 줄이기 |
| 56 | + nr_tail, nc_tail = next(tail) |
| 57 | + graph[tail[0]][tail[1]]=-1 |
| 58 | + tail[0], tail[1] = nr_tail, nc_tail |
| 59 | + else: |
| 60 | + break |
| 61 | + |
| 62 | + if command and command[0][0]==i: #게임 시작 시간으로부터 X초가 끝난 뒤 |
| 63 | + if command[0][1]=='D': |
| 64 | + snake_dir+=1 |
| 65 | + if snake_dir>3: |
| 66 | + snake_dir=0 |
| 67 | + else: |
| 68 | + snake_dir -= 1 |
| 69 | + if snake_dir<0: |
| 70 | + snake_dir=3 |
| 71 | + command.pop(0) |
| 72 | +print(i) |
0 commit comments