Study/PS(Algorithm)
[C++] 백준 BOJ 3190 뱀
노랑펭귄
2018. 9. 18. 16:42
문제링크 : https://boj.kr/3190
N*N의 정사각 보드에 빈칸은 0, 사과는 1, 뱀은 2로 표시해주었다.
뱀이 이동할 때 꼬리부터 없어진다는 규칙 때문에 뱀의 몸통은 큐(Queue)를 이용하여 저장하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | // BOJ 3190 뱀 #include <iostream> #include <vector> #include <queue> using namespace std; // 방향변화 const int dx[] = {0,1,0,-1}; const int dy[] = {1,0,-1,0}; struct pt{ int x, y; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); freopen("../input.txt", "r", stdin); int size, apple; cin >> size >> apple; vector<vector<int> > arr(size + 1, vector<int>(size + 1, 0)); int a, b; while(apple--){ cin >> a >> b; arr[a][b] = 1; // 사과 } int x = 1, y = 1; arr[x][y] = 2; // 뱀 int dir = 0; // 머리 방향 queue<pt> snake; // 뱀 몸통 snake.push({x,y}); int cnt, time; char cmd; int result = 0; cin >> cnt >> time; while(true) { result++; x += dx[dir], y += dy[dir]; // 좌표 증감 // 벽, 뱀 몸통일 경우 출력 후 exit if (x < 1 || y < 1 || x > size || y > size || arr[x][y] == 2) { cout << result; break; } // 빈칸일 경우 꼬리 제거 else if (arr[x][y] == 0) { pt t = snake.front(); snake.pop(); arr[t.x][t.y] = 0; // pop 좌표 값을 빈칸으로 } // 머리 추가 arr[x][y] = 2; snake.push({x, y}); if (result == time) { cin >> cmd; if(--cnt) // 다음 시간 cin >> time; // 방향 전환 if (cmd == 'L') dir = --dir < 0 ? 3 : dir; else dir = ++dir > 3 ? 0 : dir; } } return 0; } | cs |