Skip to content

Conversation

@Fnhid
Copy link
Collaborator

@Fnhid Fnhid commented Jul 6, 2025

πŸ”— 문제 링크

λ±€

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

자료ꡬ쑰 쀑 큐λ₯Ό μ—°μŠ΅ν•΄ λ³Ό 수 μžˆλŠ” κ΅¬ν˜„ 문제λ₯Ό κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€.

image
이 λ¬Έμ œλŠ” λ±€ κ²Œμž„μ—μ„œ, μ‚¬κ³Όμ˜ μœ„μΉ˜μ™€ λ°©ν–₯ μ „ν™˜ 정보(νšŒμ „ μ‹œμ , νšŒμ „ λ°©ν–₯)λ₯Ό μ œκ³΅ν•˜λ©΄, λͺ‡ μ΄ˆλ§Œμ— κ²Œμž„μ΄ λλ‚˜λŠ”μ§€ μ•Œμ•„λ‚΄λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

뱀은 1μ΄ˆμ— ν•œ μΉΈ 움직이고, λͺ…λ Ήμ–΄ 정보에 따라 νšŒμ „ν•©λ‹ˆλ‹€.

뱀이 λͺΈν†΅, 벽에 λΆ€λ”«νžˆλ©΄ κ²Œμž„μ΄ λλ‚©λ‹ˆλ‹€.

2차원 λ°°μ—΄λ‘œ κ²Œμž„ 판의 정보(빈 곡간, 사과, λͺΈν†΅)λ₯Ό μ €μž₯ν•˜μ˜€κ³ ,

c++의 STL인 queueλ₯Ό μ΄μš©ν•˜μ—¬ λ±€μ˜ μœ„μΉ˜ 정보λ₯Ό μ €μž₯ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

λ±€μ˜ 머리가 μƒˆλ‘œ μ΄λ™ν•˜λ©΄, κ·Έ λΆ€λΆ„μ˜ μ’Œν‘œλ₯Ό 큐에 pushν•˜κ³ ,
사과λ₯Ό λ¨Ήμ§€ μ•Šμ•˜λ‹€λ©΄ 길이가 μœ μ§€λ˜μ–΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— κΌ¬λ¦¬μ˜€λ˜ λΆ€λΆ„μ˜ μ’Œν‘œλ₯Ό popν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μˆ˜λ„ μ½”λ“œ

ν•¨μˆ˜ main
    N, K μž…λ ₯
    N x N 크기 λ³΄λ“œ 생성

    K번 반볡:
        사과 μ’Œν‘œ r, c μž…λ ₯
        λ³΄λ“œ[r-1][c-1]에 사과 ν‘œμ‹œ

    L μž…λ ₯
    λͺ…λ Ήμ–΄ λͺ©λ‘ 생성
    L번 반볡:
        μ‹œκ°„, λ°©ν–₯ 문자 μž…λ ₯
        λͺ…λ Ήμ–΄ λͺ©λ‘μ— {μ‹œκ°„, λ°©ν–₯ 문자} μ €μž₯

    κ²°κ³Ό = solve(N, λͺ…λ Ήμ–΄ λͺ©λ‘, λ³΄λ“œ)
    κ²°κ³Ό 좜λ ₯

ν•¨μˆ˜ solve(N, λͺ…λ Ήμ–΄ λͺ©λ‘, λ³΄λ“œ)
    μ‹œκ°„ = 0
    λ°©ν–₯ = 0
    λͺ…λ Ήμ–΄_순번 = 0

    머리_μ’Œν‘œ = {0, 0}
    λͺΈν†΅_큐 생성, {0, 0} μΆ”κ°€
    λ³΄λ“œ[0][0]에 λͺΈν†΅ ν‘œμ‹œ

    λ¬΄ν•œ 반볡:
        μ‹œκ°„ 1 증가
        머리_μ’Œν‘œλ₯Ό ν˜„μž¬ λ°©ν–₯으둜 이동

        λ§Œμ•½ 머리가 λ²½ λ˜λŠ” 자기 λͺΈμ— λ‹ΏμœΌλ©΄:
            반볡 쀑단

        λ§Œμ•½ μ΄λ™ν•œ 칸에 사과가 μ—†μœΌλ©΄:
            꼬리_μ’Œν‘œ = λͺΈν†΅_큐의 맨 μ•ž
            λͺΈν†΅_νμ—μ„œ 제거
            λ³΄λ“œμ—μ„œ 꼬리_μ’Œν‘œλ₯Ό 빈 칸으둜

        λͺΈν†΅_큐에 μƒˆ 머리_μ’Œν‘œ μΆ”κ°€
        λ³΄λ“œμ—μ„œ μƒˆ 머리_μ’Œν‘œλ₯Ό λͺΈν†΅μœΌλ‘œ

        λ§Œμ•½ ν˜„μž¬ μ‹œκ°„μ— μ‹€ν–‰ν•  λͺ…λ Ήμ–΄κ°€ 있으면:
            λ§Œμ•½ λͺ…령이 'D'이면, λ°©ν–₯을 였λ₯Έμͺ½μœΌλ‘œ
            μ•„λ‹ˆλ©΄, λ°©ν–₯을 μ™Όμͺ½μœΌλ‘œ
            λͺ…λ Ήμ–΄_순번 1 증가

    μ‹œκ°„ λ°˜ν™˜

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@froglike6
Copy link
Collaborator

저도 λΉ„μŠ·ν•œ λ°©μ‹μœΌλ‘œ 큐λ₯Ό μ‚¬μš©ν•΄ 이 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€. λ±€κ³Ό λ°©ν–₯을 각각 큐에 λ„£μ–΄ μž‘λ™ν•˜λ„λ‘ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.

import sys
from collections import deque
input = sys.stdin.readline

n = int(input())
k = int(input())
apple = [tuple(map(int,input().split())) for _ in range(k)]
l = int(input())
turn = deque()
for _ in range(l):
    x, c = input().split()
    turn.append((int(x), c))
    
direction = [(0, 1), (1, 0), (0, -1), (-1, 0)]
dirIndex = 0

snake = deque([(1, 1)])
occupied = {(1, 1)}
time = 0

while True:
    time += 1
    headX, headY = snake[0]
    x, y = direction[dirIndex]
    nx, ny = headX+x, headY+y
    if not(1<=nx<=n and 1<=ny<=n) or (nx, ny) in occupied:
        break
    snake.appendleft((nx, ny))
    occupied.add((nx, ny))
    
    if (nx, ny) in apple:
        apple.remove((nx, ny))
    else:
        tmpX, tmpY = snake.pop()
        occupied.remove((tmpX, tmpY))
    if turn and turn[0][0] == time:
        _, c = turn.popleft()
        if c == "L":
            dirIndex = (dirIndex-1)%4
        else:
            dirIndex = (dirIndex+1)%4
print(time)

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

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

큐λ₯Ό μ‚¬μš©ν•΄μ„œ ν‘ΈλŠ” λ°œμƒμ΄ μž¬λ°Œλ„€μš”..
κ΅¬μ‘°μ²΄λ‚˜ enum을 μ‚¬μš©ν•˜λŠ”κ±΄ μ΅μˆ™μΉ˜ μ•Šμ•„μ„œ λ‘œμš°ν•˜κ²Œ μ§°μŠ΅λ‹ˆλ‹€.

enum에 λŒ€ν•΄μ„  잘 λͺ¨λ₯΄λŠ”λ°, μœ ν˜„λ‹˜μ΄ 쑰금 μ•Œλ €μ£Όμ‹€ 수 μžˆλ‚˜μš”?

C++
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
using namespace std;

int board[101][101];
int N, K, L;
int direction = 0;
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    cin >> N >> K;
    for(int i = 0; i < K; ++i) {
        int row, col;
        cin >> row >> col;
        board[row][col] = 2;
    }
    
    cin >> L;
    queue<pair<int, char>> turns;
    for(int i = 0; i < L; ++i) {
        int t;
        char d;
        cin >> t >> d;
        turns.push({t, d});
    }
    
    int time = 0;
    deque<pair<int, int>> snake;
    snake.push_back({1, 1});
    board[1][1] = 1;
        
    while(true) {
        time++;
        int headY = snake.back().first + dy[direction];
        int headX = snake.back().second + dx[direction];
        
        if(headY <= 0 || headY > N || headX <= 0 || headX > N || board[headY][headX] == 1)
            break;
        
        if(board[headY][headX] != 2) {
            auto tail = snake.front();
            board[tail.first][tail.second] = 0;
            snake.pop_front();
        }
        
        board[headY][headX] = 1;
        snake.push_back({headY, headX});
        
        if(!turns.empty() && time == turns.front().first) {
            char dir = turns.front().second;
            if(dir == 'D')
                direction = (direction + 1) % 4;
            else
                direction = (direction + 3) % 4;
            turns.pop();
        }
    }
    
    cout << time << '\n';
    return 0;
}

@Fnhid
Copy link
Collaborator Author

Fnhid commented Jul 9, 2025

큐λ₯Ό μ‚¬μš©ν•΄μ„œ ν‘ΈλŠ” λ°œμƒμ΄ μž¬λ°Œλ„€μš”.. κ΅¬μ‘°μ²΄λ‚˜ enum을 μ‚¬μš©ν•˜λŠ”κ±΄ μ΅μˆ™μΉ˜ μ•Šμ•„μ„œ λ‘œμš°ν•˜κ²Œ μ§°μŠ΅λ‹ˆλ‹€.

enum에 λŒ€ν•΄μ„  잘 λͺ¨λ₯΄λŠ”λ°, μœ ν˜„λ‹˜μ΄ 쑰금 μ•Œλ €μ£Όμ‹€ 수 μžˆλ‚˜μš”?

enum은 κ΄€λ ¨ μžˆλŠ” μƒμˆ˜λ₯Ό λͺ¨μ•„ λ‘” 것이라고 μƒκ°ν•˜λ©΄ 될 것 κ°™μŠ΅λ‹ˆλ‹€.

각 μ—΄κ±°μžλŠ” μœ„μΉ˜μ— λ”°λΌμ„œ μžλ™μœΌλ‘œ μ •μˆ˜ 값이 ν• λ‹Ήλ˜λŠ”λ°,
첫 번째 μ—΄κ±°μžμ—λŠ” μ •μˆ˜ κ°’ 0이 ν• λ‹Ήλ˜λ©°, 각 이후 μ—΄κ±°μžμ—λŠ” 이전 μ—΄κ±°μžλ³΄λ‹€ 1 더 큰 값이 ν• λ‹Ήλ©λ‹ˆλ‹€.

μ €λŠ” μƒνƒœ 값을 가독성 있게 보여주고 싢을 λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.

enum WORDS{
  VOID, // assigned 0
  APPLE, // assigned 1
  BODY, // assigned 2
  WALL=15 // assigned 15
  BOMB, // assigned 16
} 

int board[3][5] = 1;
int board[3][5] = APPLE; // 1, 같은 μ½”λ“œμ§€λ§Œ μ½”λ“œλ₯Ό μ΄ν•΄ν•˜κΈ° μ’‹μŒ 

자료λ₯Ό μ°Έκ³ ν•˜μ‹œλ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.

@Fnhid
Copy link
Collaborator Author

Fnhid commented Jul 9, 2025

μ—¬λŸ¬ μ—΄κ±°ν˜•λ“€μ˜ 값이 μ€‘λ³΅λ˜λŠ” λ¬Έμ œλ‚˜ νƒ€μž… μ•ˆμ •μ„± λ¬Έμ œκ°€ μžˆμ–΄μ„œ enum을 μ‚¬μš©ν•˜κΈ° 살짝 κΊΌλ €μ§€λŠ” 게 μžˆμ—ˆλŠ”λ°,

c++11λΆ€ν„°λŠ” enum classλΌλŠ” κΈ°λŠ₯을 μ§€μ›ν•˜λ„€μš”. 저도 enum에 λŒ€ν•΄ μ°Ύμ•„λ³΄λ©΄μ„œ 방금 처음 μ•Œκ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μ—¬κΈ°λ₯Ό μ°Έκ³ ν•΄μ£Όμ‹œκΈ° λ°”λžλ‹ˆλ‹€.

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

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

λ±€! μΉœμˆ™ν•œ κ²Œμž„μ΄λ„€μš”..!>!>>!

λ‹€λ“€ 이걸 μ–΄μ°Œ μ΄λ ‡κ²Œ 잘 ν‘Έμ‹œλŠ”μ§€ λ”μš± λ°œμ „ν•΄λ‚˜κ°€λŠ” μ‚¬λžŒμ΄ 되고자 ν•©λ‹ˆλ‹€
queueλ₯Ό μ΄μš©ν•΄μ•Ό ν•œλ‹€λŠ” 생각을 λ°”λ‘œ λͺ» ν•΄μ„œ λ”μš± ν—€λ§Έλ˜κ²Œ μ•„λ‹Œκ°€ μ‹Άλ„€μš”
μ—¬λŸ¬ 도움을 λ°›μ•„ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.

Details

import sys
from collections import deque

input = sys.stdin.readline

N = int(input())
K = int(input())

apple_pos = set()
for _ in range(K):
    x, y = map(int, input().split())
    apple_pos.add((x - 1, y - 1))

L = int(input())
turn_time = deque()
turn_dir = deque()
for _ in range(L):
    t, d = input().split()
    turn_time.append(int(t))
    turn_dir.append(d)

snake = deque()
snake.append((0, 0))
visited = [[False] * N for _ in range(N)]
visited[0][0] = True

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
dir_idx = 0

time_now = 0
x, y = 0, 0

while True:
    time_now += 1
    nx = x + dx[dir_idx]
    ny = y + dy[dir_idx]

    if nx < 0 or ny < 0 or nx >= N or ny >= N:
        break

    if visited[nx][ny]:
        break

    snake.appendleft((nx, ny))
    visited[nx][ny] = True

    if (nx, ny) in apple_pos:
        apple_pos.remove((nx, ny))
    else:
        tx, ty = snake.pop()
        visited[tx][ty] = False

    if turn_time and time_now == turn_time[0]:
        d = turn_dir.popleft()
        turn_time.popleft()
        if d == 'D':
            dir_idx = (dir_idx + 1) % 4
        else:
            dir_idx = (dir_idx - 1 + 4) % 4

    x, y = nx, ny

print(time_now)

@Fnhid Fnhid merged commit 8a04f02 into main Jul 13, 2025
1 check passed
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.

5 participants