Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-wjdheesp44 #6

Merged
merged 2 commits into from
Apr 4, 2024
Merged

2-wjdheesp44 #6

merged 2 commits into from
Apr 4, 2024

Conversation

wjdheesp44
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

3190๋ฒˆ ๋ฑ€

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ตฌํ˜„ ๋ฌธ์ œ๋กœ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

ํฌ๊ฒŒ 3๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

  • ๋ฑ€์ด ๋ฒฝ์ด๋‚˜ ์ž๊ธฐ์ž์‹ ์—๊ฒŒ ๋ถ€๋”ชํž ๋•Œ gameover
  • ์‚ฌ๊ณผ์˜ ์œ ๋ฌด์— ๋”ฐ๋ผ ๋ฑ€์˜ ๊ธธ์ด ๋ณ€๊ฒฝ
  • X์ดˆ๊ฐ€ ๋๋‚œ ๋’ค, ํšŒ์ „(์™ผ์ชฝ or ์˜ค๋ฅธ์ชฝ)

์ˆœ์„œ

  1. ๋ฑ€์˜ ๋จธ๋ฆฌ๋ฅผ ๋‹ค์Œ์นธ์— ์œ„์น˜์‹œํ‚จ๋‹ค. (๋ฑ€์˜ ๋จธ๋ฆฌ ๋ฐฉํ–ฅ : ์˜ค๋ฅธ์ชฝ)
  2. ๋งŒ์•ฝ ๋ฒฝ์ด๋‚˜ ์ž๊ธฐ์ž์‹ ์˜ ๋ชธ๊ณผ ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ๋๋‚œ๋‹ค.
  3. ๋งŒ์•ฝ ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ทธ ์นธ์— ์žˆ๋˜ ์‚ฌ๊ณผ๊ฐ€ ์—†์–ด์ง€๊ณ  ๊ผฌ๋ฆฌ๋Š” ์›€์ง์ด์ง€ ์•Š๋Š”๋‹ค.
  4. ๋งŒ์•ฝ ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด, ๋ชธ๊ธธ์ด๋ฅผ ์ค„์—ฌ์„œ ๊ผฌ๋ฆฌ๊ฐ€ ์œ„์น˜ํ•œ ์นธ์„ ๋น„์›Œ์ค€๋‹ค. ์ฆ‰, ๋ชธ๊ธธ์ด๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์‚ฌ๊ณผ๊ฐ€ ์žˆ๋Š” ์นธ = 2
๋ฑ€์ด ์žˆ๋Š” ์นธ = 1
์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์นธ = 0
์ด๋Ÿฐ ์‹์œผ๋กœ ์นธ์„ ๊ตฌ๋ณ„ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์ œ์—์„œ ์‚ฌ๊ณผ๋Š” ๋นจ๊ฐ„ ๋ฐ”ํƒ•์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ1

๋ฒฝ์— ๋ถ€๋”ชํ˜€์„œ gameover ๋ฉ๋‹ˆ๋‹ค.

image

์˜ˆ์ œ2

๋ฒฝ์— ๋ถ€๋”ชํ˜€์„œ gameover ๋ฉ๋‹ˆ๋‹ค.

image

์˜ˆ์ œ3

์ž๊ธฐ์ž์‹ ์—๊ฒŒ ๋ถ€๋”ชํ˜€์„œ gameover ๋ฉ๋‹ˆ๋‹ค. (๋ฑ€ ๊ธธ์ด ๋•Œ๋ฌธ์—)

image


import sys
input = sys.stdin.readline

visited = []
x, y = 1, 1
dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]
angle = 1
X = 0
snake_length = 1
target = 0

n = int(input())
board = [[0] * (n+1) for _ in range(n+1)]

apple_cnt = int(input())
apple = []
for i in range(apple_cnt):
    apple.append(list(map(int, input().split())))

for i, j in apple:
    board[i][j] = 2

dir_cnt = int(input())
dir = []
for i in range(dir_cnt):
    dir.append(list(map(str, input().split())))

while True:
    visited.append((x, y))
    nx = x + dx[angle]
    ny = y + dy[angle]
    X += 1

    if nx <= 0 or nx > n or ny <= 0 or ny > n or board[nx][ny] == 1:    # ๋ฑ€์ด ๋ฒฝ์ด๋‚˜ ์ž๊ธฐ์ž์‹ ์—๊ฒŒ ๋ถ€๋”ชํžˆ๋ฉด gameover
        print(X)
        break
    
    x, y = nx, ny

    if board[x][y] == 2:    # ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด -> ๋ฑ€ ๊ธธ์ด ๋Š˜๋ ค์คŒ
        board[x][y] = 1
        snake_length += 1
    elif board[x][y] == 0:  # ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด -> ๊ผฌ๋ฆฌ ๋ถ€๋ถ„ ๋น„์›Œ์คŒ
        board[x][y] = 1
        past_x, past_y = visited[X - snake_length]
        board[past_x][past_y] = 0
    

    if int(dir[target][0]) == X:    # X์ดˆ๊ฐ€ ๋๋‚œ ๋’ค(ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์ฃผ์–ด์ง„ ์‹œ๊ฐ„ ๋น„๊ต), ํšŒ์ „
        if dir[target][1] == 'L':
            angle = (angle + 3) % 4
        elif dir[target][1] == 'D':
            angle = (angle + 1) % 4
        
        if target < dir_cnt - 1:
            target += 1

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณผ ๋•Œ, ๋ฑ€์ด ์ด๋™ํ•œ ํ›„ ์ดˆ๋ฅผ ์„ธ๋Š”์ง€, ์ดˆ๋ฅผ ์„ธ๊ณ  ์ด๋™ํ•˜๋Š”์ง€ ํ—ท๊ฐˆ๋ ธ์Šต๋‹ˆ๋‹ค... ๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ๊ณ  ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ๋Š” ๊ทธ๋ฆผ์„ ์ •ํ™•ํžˆ ๊ทธ๋ ค์•ผ ์ฝ”๋“œ๋ฅผ ์งœ๊ธฐ ์ข‹๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ๊ธˆ ๊นจ๋‹ซ๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”.

Copy link
Collaborator

@erase-jeong erase-jeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ํฌ์ˆ˜๋‹˜์ด ๋งํ•˜์‹ ๊ฑฐ์ฒ˜๋Ÿผ ๋ฑ€์ด ์ด๋™ํ•œ ํ›„ ์ดˆ๋ฅผ ์„ธ๋Š”์ง€, ์ดˆ๋ฅผ ์„ธ๊ณ  ์ด๋™ํ•˜๋Š”์ง€ ํ—ท๊ฐˆ๋ ธ์–ด์š”. ์ €๋Š” ๋ฌธ์ œ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜์„œ ์ž‘์„ฑํ•ด์ฃผ์‹  ์ˆ˜๋„์ฝ”๋“œ ๋ณด๊ณ  ์ดํ•ดํ–ˆ๋„ค์š”. ๊ผผ๊ผผํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ์…จ๋„ค์š”!

Copy link
Collaborator

@Me-in-U Me-in-U left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ ‡๊ฒŒ ์ด๋™ํ•˜๋Š” ๋ฌธ์ œ๋Š” ์—ญ์‹œ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๋ฉด์„œ ํ’€๋ฉด ๋” ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์ฃ .

while (true) {
  time++;
  nextPos();
  if (!(wallCheck() && moveSnake()))
    break;
  appleCheck();
  turn();
}

์ €๋Š” ๊ธฐ๋Šฅ๋ณ„๋กœ ์ด๋ ‡๊ฒŒ ํ–ˆ๋Š”๋ฐ ์ทจํ–ฅ์ฐจ์ด๊ฒ ์ฃ 

Copy link
Member

@SunYerim SunYerim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•ˆ๋…•ํ•˜์„ธ์š” ๋ฆฌ๋ทฐ๊ฐ€ ๋„ˆ๋ฌด ๋Šฆ์–ด์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค ๐Ÿฅฒ
์šฐ์„  ์ €๋„ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์˜ˆ์ „์— ํ’€์–ด๋ณธ ์ ์ด ์žˆ์–ด์„œ ๊ทธ๋•Œ์˜ ๊ธฐ์–ต์„ ๋˜์‚ด๋ ค๊ฐ€๋ฉฐ ํฌ์ˆ˜๋‹˜์ด ์˜ฌ๋ ค์ฃผ์‹  ๋‚ด์šฉ์„ ๋ดค์Šต๋‹ˆ๋‹ค! ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ•ด์ฃผ์…”์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋” ๋ช…ํ™•ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋„ค์šฅ ๐Ÿ™‚

์ €๋„ ์ œ๊ฐ€ ํ’€์—ˆ๋˜ ์ฝ”๋“œ์™€ ๋กœ์ง์„ ๊ฐ„๋žตํ•˜๊ฒŒ ๋ง๋ถ™์—ฌ ๋ฆฌ๋ทฐ ๋งˆ๋ฌด๋ฆฌํ• ๊ฒŒ์š” ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ~

from collections import deque


def change(d, c):
    # ์ƒ(0) ์šฐ(1) ํ•˜(2) ์ขŒ(3)
    # ๋™์ชฝ ํšŒ์ „: ์ƒ(0) -> ์šฐ(1) -> ํ•˜(2) -> ์ขŒ(3) -> ์ƒ(0) : +1 ๋ฐฉํ–ฅ
    # ์™ผ์ชฝ ํšŒ์ „: ์ƒ(0) -> ์ขŒ(3) -> ํ•˜(2) -> ์šฐ(1) -> ์ƒ(0) : -1 ๋ฐฉํ–ฅ
    if c == "L":
        d = (d - 1) % 4
    else:
        d = (d + 1) % 4
    return d


# ์ƒ ์šฐ ํ•˜ ์ขŒ
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]


def start():
    direction = 1  # ์ดˆ๊ธฐ ๋ฐฉํ–ฅ
    time = 1  # ์‹œ๊ฐ„
    y, x = 0, 0  # ์ดˆ๊ธฐ ๋ฑ€ ์œ„์น˜
    visited = deque([[y, x]])  # ๋ฐฉ๋ฌธ ์œ„์น˜
    arr[y][x] = 2
    while True:
        y, x = y + dy[direction], x + dx[direction]
        if 0 <= y < N and 0 <= x < N and arr[y][x] != 2:
            if not arr[y][x] == 1:  # ์‚ฌ๊ณผ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
                temp_y, temp_x = visited.popleft()
                arr[temp_y][temp_x] = 0  # ๊ผฌ๋ฆฌ ์ œ๊ฑฐ
            arr[y][x] = 2
            visited.append([y, x])
            if time in times.keys():
                direction = change(direction, times[time])
            time += 1
        else:  # ๋ณธ์ธ ๋ชธ์— ๋ถ€๋”ชํžˆ๊ฑฐ๋‚˜, ๋ฒฝ์— ๋ถ€๋”ชํžŒ ๊ฒฝ์šฐ
            return time


if __name__ == "__main__":

    # input
    N = int(input())
    K = int(input())
    arr = [[0] * N for _ in range(N)]
    for _ in range(K):
        a, b = map(int, input().split())
        arr[a - 1][b - 1] = 1  # ์‚ฌ๊ณผ ์ €์žฅ
    L = int(input())
    times = {}
    for i in range(L):
        X, C = input().split()
        times[int(X)] = C
    print(start())
  1. change(d, c) -> ๋ฑ€์˜ ๋ฐฉํ–ฅ์„ ๋ณ€๊ฒฝํ•˜๋Š” ์—ญํ• , ํ˜„์žฌ ๋ฐฉํ–ฅ - d, ํšŒ์ „ํ•  ๋ฐฉํ–ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž - c
  2. start() -> ๊ฒŒ์ž„์ด ์‹œ์ž‘๋˜๋Š” ์ง€์ . ๋ฑ€์ด ์ด๋™ํ•˜๋ฉด์„œ ๊ฒŒ์ž„์ด ์ง„ํ–‰ ๋œ๋‹ค.
  • ํ˜„์žฌ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋‹ค์Œ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐ
  • ๋‹ค์Œ ์œ„์น˜๊ฐ€ ์œ ํšจํ•œ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ , ๋ฑ€์˜ ๋ชธํ†ต์ด ์—†๋Š” ์œ„์น˜๋ผ๋ฉด
    • ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ, ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด ๋ฑ€์˜ ๊ผฌ๋ฆฌ๋ฅผ ์ค„์ž„.
    • ๋ฑ€์˜ ๋จธ๋ฆฌ๋ฅผ ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™์‹œํ‚ด.
    • ์ด๋™ํ•œ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ , ๋ฑ€์˜ ๋ชธํ†ต ์œ„์น˜๋ฅผ ๊ธฐ๋ก.
    • ์‹œ๊ฐ„์ด ํŠน์ • ์‹œ๊ฐ„๋Œ€์— ๋„๋‹ฌํ•˜๋ฉด ๋ฐฉํ–ฅ์„ ๋ณ€๊ฒฝ.
  • ๋ฒฝ์ด๋‚˜ ๋ฑ€์˜ ๋ชธํ†ต์— ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๊ณ  ํ˜„์žฌ ์‹œ๊ฐ„ ๋ฐ˜ํ™˜

@wjdheesp44 wjdheesp44 merged commit 2a5d884 into main Apr 4, 2024
@wjdheesp44 wjdheesp44 deleted the 2-wjdheesp44 branch April 4, 2024 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants