혼자 정리
3190번: 뱀 본문
C++
#include <iostream>
#include <queue>
using namespace std;
#define APPLE -1
#define DOWN 2
#define LEFT 3
#define UP 4
#define RIGHT 5
int N, K;
int apple_pos[100][2];
int L;
int board[101][101];
int move_int[100];
char move_char[100];
pair<int, int> head_pos;
pair<int, int> tail_pos;
queue<pair<int, char> > q;
int elapsed_sec;
int direction = RIGHT;
void game(){
while (1){
elapsed_sec++;
if (direction == RIGHT){
head_pos.second++;
}else if (direction == LEFT){
head_pos.second--;
}else if (direction == UP){
head_pos.first--;
}else if (direction == DOWN){
head_pos.first++;
}
if (head_pos.first == 0 || head_pos.first > N || head_pos.second == 0 || head_pos.second > N) // 벽에 부딪힌 경우
return ;
else if (board[head_pos.first][head_pos.second] > 0) // 몸통과 부딪힌 경우
return ;
if (board[head_pos.first][head_pos.second] == 0){
int tmp = board[tail_pos.first][tail_pos.second];
board[tail_pos.first][tail_pos.second] = 0;
if (tmp == DOWN){
tail_pos.first++;
}else if (tmp == UP){
tail_pos.first--;
}else if (tmp == LEFT){
tail_pos.second--;
}else if (tmp == RIGHT){
tail_pos.second++;
}
}
if (elapsed_sec == q.front().first){
char tmp = q.front().second;
q.pop();
if (tmp == 'L'){
if (direction == DOWN){
direction = RIGHT;
}else{
direction--;
}
}else{
if (direction == RIGHT){
direction = DOWN;
}else{
direction++;
}
}
}
board[head_pos.first][head_pos.second] = direction;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 0; i < K; i++){
cin >> apple_pos[i][0] >> apple_pos[i][1];
}
cin >> L;
for (int i = 0; i < L; i++){
int tmp_int;
char tmp_char;
cin >> tmp_int >> tmp_char;
q.push({tmp_int, tmp_char});
}
for (int i = 0; i < K; i++){
board[apple_pos[i][0]][apple_pos[i][1]] = -1;
}
board[1][1] = RIGHT;
head_pos.first = 1, head_pos.second = 1;
tail_pos.first = 1, tail_pos.second = 1;
game();
cout << elapsed_sec << "\n";
}
- 파이썬은 89퍼쯤에서 런타임에러(index error) : 이유 못 찾음..ㅠㅠ
'알고리즘 문풀 > 백준' 카테고리의 다른 글
14891번: 톱니바퀴 (0) | 2021.08.06 |
---|---|
14890번: 경사로 (0) | 2021.08.06 |
14889번: 스타트와 링크 (0) | 2021.08.05 |
14888번: 연산자 끼워넣기 (0) | 2021.08.05 |
14502번: 연구소 (0) | 2021.08.05 |