-
Notifications
You must be signed in to change notification settings - Fork 1
19-fnhid #74
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
Conversation
|
μ λ λΉμ·ν λ°©μμΌλ‘ νλ₯Ό μ¬μ©ν΄ μ΄ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€. λ±κ³Ό λ°©ν₯μ κ°κ° νμ λ£μ΄ μλνλλ‘ κ΅¬ννμ΅λλ€. 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) |
dohyeondol1
left a comment
There was a problem hiding this 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;
}
enumμ κ° μ΄κ±°μλ μμΉμ λ°λΌμ μλμΌλ‘ μ μ κ°μ΄ ν λΉλλλ°, μ λ μν κ°μ κ°λ μ± μκ² λ³΄μ¬μ£Όκ³ μΆμ λ μ¬μ©ν©λλ€. 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, κ°μ μ½λμ§λ§ μ½λλ₯Ό μ΄ν΄νκΈ° μ’μ
μλ£λ₯Ό μ°Έκ³ νμλ©΄ μ’μ κ² κ°μ΅λλ€. |
|
μ¬λ¬ μ΄κ±°νλ€μ κ°μ΄ μ€λ³΅λλ λ¬Έμ λ νμ μμ μ± λ¬Έμ κ° μμ΄μ enumμ μ¬μ©νκΈ° μ΄μ§ κΊΌλ €μ§λ κ² μμλλ°, c++11λΆν°λ enum classλΌλ κΈ°λ₯μ μ§μνλ€μ. μ λ enumμ λν΄ μ°Ύμ보면μ λ°©κΈ μ²μ μκ² λμμ΅λλ€. μ¬κΈ°λ₯Ό μ°Έκ³ ν΄μ£ΌμκΈ° λ°λλλ€. |
hadongun
left a comment
There was a problem hiding this 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)
π λ¬Έμ λ§ν¬
λ±
βοΈ μμλ μκ°
30λΆ
β¨ μλ μ½λ
μλ£κ΅¬μ‘° μ€ νλ₯Ό μ°μ΅ν΄ λ³Ό μ μλ ꡬν λ¬Έμ λ₯Ό κ°μ Έμμ΅λλ€.
μ΄ λ¬Έμ λ λ± κ²μμμ, μ¬κ³Όμ μμΉμ λ°©ν₯ μ ν μ 보(νμ μμ , νμ λ°©ν₯)λ₯Ό μ 곡νλ©΄, λͺ μ΄λ§μ κ²μμ΄ λλλμ§ μμλ΄λ λ¬Έμ μ λλ€.
λ±μ 1μ΄μ ν μΉΈ μμ§μ΄κ³ , λͺ λ Ήμ΄ μ 보μ λ°λΌ νμ ν©λλ€.
λ±μ΄ λͺΈν΅, λ²½μ λΆλ«νλ©΄ κ²μμ΄ λλ©λλ€.
2μ°¨μ λ°°μ΄λ‘ κ²μ νμ μ 보(λΉ κ³΅κ°, μ¬κ³Ό, λͺΈν΅)λ₯Ό μ μ₯νμκ³ ,
c++μ STLμΈ queueλ₯Ό μ΄μ©νμ¬ λ±μ μμΉ μ 보λ₯Ό μ μ₯νμμ΅λλ€.
λ±μ λ¨Έλ¦¬κ° μλ‘ μ΄λνλ©΄, κ·Έ λΆλΆμ μ’νλ₯Ό νμ pushνκ³ ,
μ¬κ³Όλ₯Ό λ¨Ήμ§ μμλ€λ©΄ κΈΈμ΄κ° μ μ§λμ΄μΌ νκΈ° λλ¬Έμ 꼬리μλ λΆλΆμ μ’νλ₯Ό popνμμ΅λλ€.
μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©