Algorithm/Baekjoon
[백준][C++]3190번 뱀
연모링
2023. 6. 21. 23:26
728x90
#include <iostream>
#include <queue>
#include <string>
using namespace std;
bool map[101][101];
int n, k, l; // 보드 크기, 사과 개수, 방향 변환 횟수
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
bool apple[101][101];
int dir[10001];
int d;
int main()
{
// 뱀은 큐. 뱀의 길이가 선입 선출
queue<pair<int, int>> snake;
cin >> n >> k;
for (int i = 0; i < k; i++)
{
int x, y;
cin >> x >> y;
apple[x][y] = true;
}
cin >> l;
for (int i = 0; i < l; i++)
{
int x;
string s;
cin >> x >> s;
if (s == "L")
{
dir[x] = -1; // 왼쪽으로 회전
}
else
{
dir[x] = 1; // 오른쪽으로 회전
}
}
int time = 0;
snake.push(make_pair(1, 1));
map[1][1] = true;
d = 0;
while (1)
{
time++;
int x = snake.back().first;
int y = snake.back().second;
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 1 || nx > n || ny < 1 || ny > n)
{
break;
}
snake.push(make_pair(nx, ny));
if (dir[time] == 1)
{
d += 1;
if (d == 4) d = 0;
}
else if (dir[time] == -1)
{
d -= 1;
if (d == -1) d = 3;
}
if (apple[nx][ny])
{
apple[nx][ny] = false;
}
else
{
if (map[nx][ny])
{
break;
}
map[snake.front().first][snake.front().second] = false;
snake.pop();
}
map[nx][ny] = true;
}
cout << time;
return 0;
}
좀 여러 번 틀린 문제였다.
틀린 이유
1. 연산자 오타(어이- 정신차리라구!)
=쓰는 부분에 ==로 적힌 게 한 곳 있었다.
2. 뱀의 좌표를 꺼낼 때 뱀 머리에서 꺼내 와야하는데 꼬리에서 꺼냄
3. 행 헷갈림ㅋㅋ <이게 제일 결정적인 듯?
실수한 부분 찾느라고 눈 빠지도록 모니터 째려봤다

눈이 아프다. 머리가 나쁘면 몸이 고생한다더니...
실수를 육체적 고통으로 몸에 새겨뒀으니 이제 다시는 이런 부분에서 안 틀릴 수 있을 것 같다~~

참고로 뱀을 풀고나서 백준 골드 3이 됐다.

'예전의 나'보다는 확실히 잘하게 됐지만
골드 3이라고 할 만큼 잘하는 건 아닌 것 같다...
앞으로 더 수련하겠습니다
728x90