Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 그리디 알고리즘
- 백트래킹
- 알고리즘
- 스택
- 재귀
- 우선순위 큐
- 투 포인터
- 다이나믹 프로그래밍
- 백준
- 트리
- 유니온 파인드
- 자료구조
- 누적 합
- 그래프
- 구현
- XR Interaction Toolkit
- DFS
- 다익스트라
- 유니티
- VR
- 수학
- c++
- Unreal Engine 5
- 문자열
- BFS
- 시뮬레이션
- 브루트포스
- ue5
- Team Fortress 2
- 정렬
Archives
- Today
- Total
1일1알
백준 3190번 뱀 C++ 본문
뱀이 이동할 때마다 위치를 큐에 넣고, 보드의 정보를 갱신해준다. 만약 이동한 위치에 사과가 없다면 몸의 크기는 그대로 유지해야 하기 때문에 pop을 해주고 pop한 위치를 아무것도 없는 칸으로 바꿔준다.
사과가 있다면 몸의 크기가 늘어나기 때문에 pop을 하지 않고 진행하면 된다.
그리고 회전 정보를 이용하여 뱀을 회전시키고, 다음 위치가 자신의 몸이거나 벽이면 반복문을 종료한다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int n, k, l;
vector<vector<int>> board(101, vector<int>(101, 0));
vector<pair<int, char>> rotates;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cin >> k;
for (int i = 0; i < k; i++) {
int r, c;
cin >> r >> c;
board[r][c] = 2;
}
cin >> l;
for (int i = 0; i < l; i++) {
int cnt;
char rot;
cin >> cnt >> rot;
rotates.push_back({ cnt,rot });
}
int row = 1;
int col = 1;
queue<pair<int, int>> q;
board[1][1] = 1;
q.push({ 1,1 });
int dir = 1;
int cnt = 0;
int rotIdx = 0;
while (true) {
cnt++;
int nextRow = row + dRow[dir];
int nextCol = col + dCol[dir];
if (nextRow<1 || nextRow>n) break;
if (nextCol<1 || nextCol>n) break;
if (board[nextRow][nextCol] == 1) break;
if (board[nextRow][nextCol] == 0) {
auto back = q.front();
q.pop();
board[back.first][back.second] = 0;
}
board[nextRow][nextCol] = 1;
q.push({ nextRow,nextCol });
row = nextRow;
col = nextCol;
if (rotIdx<rotates.size() && cnt == rotates[rotIdx].first) {
if (rotates[rotIdx].second == 'D') {
dir = (dir + 1) % 4;
}
else {
dir = (dir + 3) % 4;
}
rotIdx++;
}
}
cout << cnt;
};
'알고리즘' 카테고리의 다른 글
백준 16234번 인구 이동 C++ (0) | 2022.01.03 |
---|---|
백준 2225번 합분해 C++ (0) | 2022.01.02 |
백준 15686번 치킨 배달 C++ (0) | 2021.12.31 |
백준 14503번 로봇 청소기 C++ (0) | 2021.12.30 |
백준 7562번 나이트의 이동 C++ (0) | 2021.12.29 |