백준 3190 - 뱀

https://www.acmicpc.net/problem/3190

체점 현황

전체 소스 코드

#include <bits/stdc++.h>
using namespace std;

int board[110][110];
int board_size;
int num_of_apple;
int num_of_command;

map<int, char> command;

// 동, 남, 서, 북
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int direction[4] = {0, 1, 2, 3};

struct snake {
int y;
int x;
int dir;
};

int main(void) {
cin >> board_size >> num_of_apple;

for (int i = 0; i < num_of_apple; i++) {
int y, x;
cin >> y >> x;
board[y][x] = 1;
}

cin >> num_of_command;
for (int i = 0; i < num_of_command; i++) {
int time;
char dir;
cin >> time >> dir;
command[time] = dir;
}

queue<pair<int, int>> snake_tail;
int snake_head_y = 1;
int snake_haed_x = 1;
int snake_dir = 0;
snake_tail.push({1, 1});
board[1][1] = 2;

int time = 0;
while (true) {
time++;
snake_head_y += dy[snake_dir];
snake_haed_x += dx[snake_dir];
snake_tail.push({snake_head_y, snake_haed_x});

if (board[snake_head_y][snake_haed_x] == 2) {
cout << time << '\n';
return 0;
}

if (0 >= snake_head_y || snake_head_y > board_size || 0 >= snake_haed_x || snake_haed_x > board_size) {
cout << time << '\n';
return 0;
}

if (board[snake_head_y][snake_haed_x] == 1) {
board[snake_head_y][snake_haed_x] = 2;
} else {
board[snake_head_y][snake_haed_x] = 2;
board[snake_tail.front().first][snake_tail.front().second] = 0;
snake_tail.pop();
}

if (command.find(time) != command.end()) {
char com = command[time];
command.erase(time);

if (com == 'L') {
snake_dir = (snake_dir + 3) % 4;
} else {
snake_dir = (snake_dir + 1) % 4;
}
}
}

return 0;
}
Share