๋ฌธ์
ํ์ด
#include <iostream>
#include <utility>
#include <queue>
using namespace std;
int N;
int appleNum;
int rotateNum;
int map[101][101];
vector<pair<int, int>> Snake;
queue<pair<int, int>> Rotate;
int currentTime = 1;
int currentDir = 2;
// ํด๋น ์๊ฐ์ ๋ฐฉํฅ์ ์ ํ
int changeDirection(int time, int dir) {
if (Rotate.front().first == time) {
dir += Rotate.front().second;
if (dir == 5)
dir = 1;
if (dir == 0)
dir = 4;
Rotate.pop();
}
return dir;
}
// ๋ฑ์ ๊ท์น์ ๋ง๊ฒ ์ด๋
bool moveSnake() {
int current_head_I = Snake.back().first;
int current_head_J = Snake.back().second;
// ๋ค์ head์ ์ขํ ์ฐพ๊ธฐ
int next_head_I;
int next_head_J;
switch (currentDir) {
case 1: // ์
next_head_I = -1;
next_head_J = 0;
break;
case 2: // ์ฐ
next_head_I = 0;
next_head_J = 1;
break;
case 3: // ํ
next_head_I = 1;
next_head_J = 0;
break;
case 4: // ์ข
next_head_I = 0;
next_head_J = -1;
break;
}
next_head_I += current_head_I;
next_head_J += current_head_J;
// head์ ๋ฒฝ ์ถฉ๋ ๊ฒ์ฌ
if (next_head_I > N || next_head_J > N || next_head_I < 1 || next_head_J < 1) {
return false;
}
// head์ ์๊ธฐ์์ ์ถฉ๋ ๊ฒ์ฌ
for (int i = 0; i < Snake.size() - 1; i++) {
if (Snake[i].first == next_head_I && Snake[i].second == next_head_J) {
return false;
}
}
// head๋ฅผ ์ถ๊ฐํด์ฃผ๊ณ
Snake.push_back(make_pair(next_head_I, next_head_J));
// head์์น์ ์ฌ๊ณผ๊ฐ ์๋ค๋ฉด ๊ผฌ๋ฆฌ๋ฅผ ์ญ์
if (map[next_head_I][next_head_J] != -1) {
Snake.erase(Snake.begin());
}
// head์์น์ ์ฌ๊ณผ๊ฐ ์๋ค๋ฉด ์ฌ๊ณผ๋ฅผ ์ญ์
else {
map[next_head_I][next_head_J] = 0;
}
return true;
}
int main() {
cin >> N;
// ์ฌ๊ณผ ์์น ์
๋ ฅ
int getI;
int getJ;
cin >> appleNum;
for (int i = 0; i < appleNum; i++) {
cin >> getI >> getJ;
map[getI][getJ] = -1;
}
// ํ์ ์ ๋ณด ์
๋ ฅ
int time;
char Dir;
pair<int, int> input;
cin >> rotateNum;
for (int i = 0; i < rotateNum; i++) {
cin >> time >> Dir;
input.first = time;
// ์ค๋ฅธ์ชฝ์ ์๊ณ๋ฐฉํฅ(+1), ์ผ์ชฝ์ ๋ฐ์๊ณ๋ฐฉํฅ(-1)
if (Dir == 'L') input.second = -1;
else if (Dir == 'D') input.second = 1;
Rotate.push(input);
}
Snake.push_back(make_pair(1, 1));
// moveSnake๊ฐ ์ข
๋ฃ์กฐ๊ฑด์ ์ ๊ฑธ๋ฆฌ๋ฉด ๊ณ์ ๋ฑ ์ด๋
while (moveSnake()) {
if (Rotate.size() > 0) {
currentDir = changeDirection(currentTime, currentDir);
}
currentTime++;
}
cout << currentTime << endl;
return 0;
}
๋ฑ์ด ๊ท์น์ ๋ง๊ฒ ์์ง์ด๋ ํจ์๋ฅผ ์ข
๋ฃ ์กฐ๊ฑด์ ๊ฑธ๋ ธ๋์ง ์ ๊ฑธ๋ ธ๋์ง๋ฅผ bool๊ฐ์ returnํ๋๋ก ๊ตฌํํ๋ค.
๋ฐฉํฅ์ 1, 2, 3, 4๋ฅผ ์, ์ฐ, ํ, ์ข(์๊ณ๋ฐฉํฅ) ์์๋๋ก ์ง์ ํ์ฌ ํด๋น ์๊ฐ์ ๋ฐฉํฅ์ ๋ฐ๊ฟ์ฃผ๋๋ก ํ๋ค.
โ๋๊ฐ์ ํจ์๋ฅผ while๋ฌธ์ผ๋ก ๊ฐ๋จํ๊ฒ ํฉ์ณค๊ณ , ํ ๋ฒ loop์ ๋ ๋๋ง๋ค ์๊ฐ์ ์ฆ๊ฐ์์ผ์คฌ๋ค.
โํ์๊ณผ ๋ฐฉํฅ์ ๋ค๋ฅด๊ฒ ์งฐ์ผ๋, ์๋ก ์ง ์ฝ๋๋ฅผ ๊ธ๋ฐฉ ์ดํดํ๊ณ ๋์์ ์ค์ ์ฝ๋๊ฐ ๊น๋ํ๊ฒ ์ง์ฌ์ง ๊ฒ ๊ฐ์ ์ข์๋ค.
'๐ > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ] ์ค๋ชฉ (๋ฐฑ์ค, 2615) (0) | 2022.01.25 |
---|---|
[์๊ณ ๋ฆฌ์ฆ] ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (๋ฐฑ์ค, 2941) (0) | 2021.03.03 |
[์๊ณ ๋ฆฌ์ฆ] โพ (๋ฐฑ์ค, 17281) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] 2048(Easy) (๋ฐฑ์ค, 12100) (0) | 2020.11.20 |
[์๊ณ ๋ฆฌ์ฆ] ์ฐ๊ตฌ์ (๋ฐฑ์ค, 14502) (0) | 2020.11.20 |