본문 바로가기
백준 알고리즘/시뮬레이션

백준 3190번 C++

by paysmile 2019. 1. 29.


#include<iostream>

#include<cstring>

#include<deque>

using namespace std;


const int MAX = 102;

int n, k;

int apple[MAX][MAX];

int l;

pair<int, char> movedir[101];

int currentdir = 1;

class Point {

public:

int x, y;

Point(int x, int y) {

this->x = x;

this->y = y;

}

};

deque<Point> snake;


bool terminatecheck() {

if (snake.front().x <1 || snake.front().x > n || snake.front().y > n || snake.front().y <1)

return true;

if (apple[snake.front().x][snake.front().y] == 2) 

return true;

return false;

}


void changedir(int i) {

switch (currentdir) {

case 1:

if (movedir[i].second == 'D')

currentdir = 2;

else

currentdir = 4;

break;

case 2:

if (movedir[i].second == 'D')

currentdir = 3;

else

currentdir = 1;

break;

case 3:

if (movedir[i].second == 'D')

currentdir = 4;

else

currentdir = 2;

break;

case 4:

if (movedir[i].second == 'D')

currentdir = 1;

else

currentdir = 3;

break;

}

}

int snakemove() {

int index = 0;

int time = 0;


for (int i = 0; i < 10005; i++) {

if ((i == movedir[index].first) && (index < l)) {

changedir(index);

index += 1;

}

switch (currentdir) {

case 1:

snake.push_front(Point(snake.front().x, snake.front().y + 1));

break;

case 2:

snake.push_front(Point(snake.front().x+1, snake.front().y));

break;

case 3:

snake.push_front(Point(snake.front().x, snake.front().y - 1));

break;

case 4:

snake.push_front(Point(snake.front().x-1, snake.front().y));

break;

}

time++;

if (terminatecheck()) {

return time;

}

if (apple[snake.front().x][snake.front().y] != 1){

apple[snake.back().x][snake.back().y] = 0;

snake.pop_back();

}

apple[snake.front().x][snake.front().y] = 2;

}

return time;

}

int main(void) {

cin >> n >> k;

memset(apple, 0, sizeof(apple));

for (int i = 0; i < k; i++) {

int x, y;

cin >> x >> y;

apple[x][y] = 1;

}

cin >> l;

for (int i = 0; i < l; i++) {

int time;

char dirs;

cin >> time;

cin >> dirs;

movedir[i] = make_pair(time, dirs);

}

snake.push_back(Point(1, 1));

apple[1][1] = 2;

cout << snakemove() << endl;

}

'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글

백준 14499번 C++  (0) 2019.04.07
백준 3190 C++  (0) 2019.04.07
백준 14499번 C++  (0) 2019.01.27
백준 14503번 C++  (0) 2019.01.26
백준 1547 파이썬  (0) 2018.11.13