Tags
- queue
- dfs
- stack
- ์ ๋ ฌ
- ์๋ฃ๊ตฌ์กฐ
- DP
- sort
- ๊น์ด ์ฐ์ ํ์
- ๊ทธ๋ํ ์ด๋ก
- ์๋ฎฌ๋ ์ด์
- ๊ต์ฌ
- BOJ
- ๊ทธ๋ํ ํ์
- Brute Force Algorithm
- SpringBoot
- BFS
- ์ ์๋ก
- CodingTest
- LV2
- ๋ฐฑํธ๋ํน
- Python
- greedy
- ๋๋น ์ฐ์ ํ์
- Dynamic Programming
- ๋ฌธ์์ด
- ์ํ
- Study
- Java
- ๊ตฌํ
- PGM
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_14 : ํฑ๋๋ฐํด ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- ๋ฌธ์ ๊ฐ ์ข ๊ธธ์์ง๋ง ์ด๋ ค์ด ์กฐ๊ฑด์ ์๋๋ค. 4๊ฐ์ง ํฑ๋๊ฐ ์๋๋ฐ, ๊ทธ ์ค ํ๋๋ฅผ ๋๋ ธ์๋ ๋ค๋ฅธ ํฑ๋ ์ํ๊ฐ ์ด๋ป๊ฒ ๋ณํ๋์ง ๊ตฌํ๋ฉด ๋๋ค.
- ๊ฐ ํฑ๋ ์ํ๋ vector ๋ฅผ ์ฐ๋ฉด ๋ ๊ฒ ๊ฐ๊ณ , ์ ํฑ๋์๊ฒ ์ํฅ์ ์ฃผ๋ ๊ฒ์ ๋ง์น BFS์ ์๋ฆฌ์ ๋น์ทํ ๊ฒ ๊ฐ์์ queue๋ฅผ ์ฌ์ฉํ๋๋ ์ฝ๊ฒ ํ๋ ธ๋ค.
๐ธ ์ฝ๋ ๐ธ
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
void turn(vector<int>& top, int& direction) {
int temp;
// turn right
if (direction == 1) {
temp = top[7];
for (int i = 7; i > 0; i--) {
top[i] = top[i - 1];
}
top[0] = temp;
}
// turn left
else {
temp = top[0];
for (int i = 0; i < 7; i++) {
top[i] = top[i + 1];
}
top[7] = temp;
}
}
int main(void) {
string status;
vector<vector<int>>top(4, vector<int>(8));
for (int i = 0; i < 4; i++)
{
cin >> status;
for (int j = 0; j < 8; j++)
{
top[i][j] = status[j] - '0';
}
}
int K, Tnum, direction;
cin >> K;
queue<int> q;
for (int i = 0; i < K; i++)
{
int flag[4] = { 0 };
cin >> Tnum >> direction;
q.push(Tnum - 1);
flag[Tnum - 1] = direction;
while (!q.empty()) {
int now = q.front();
int left = top[now][6];
int right = top[now][2];
turn(top[now], flag[now]);
if (now - 1 >= 0) { // ์ผ์ชฝ ํฑ๋๋ฐํด ์กด์ฌ
// ์ผ์ชฝ ํฑ๋๋ฐํด๊ฐ ์์ง ๋์๊ฐ์ง ์์๊ณ , ์ ์ ์ ๊ทน์ด ๋ฐ๋
if (flag[now - 1] == 0 && top[now - 1][2] != left) {
q.push(now - 1);
flag[now - 1] = flag[now] * -1;
}
}
if (now + 1 <= 3) { // ์ค๋ฅธ์ชฝ ํฑ๋๋ฐํด ์กด์ฌ
// ์ค๋ฅธ์ชฝ ํฑ๋๋ฐํด๊ฐ ์์ง ๋์๊ฐ์ง ์์๊ณ , ์ ์ ์ ๊ทน์ด ๋ฐ๋
if (flag[now + 1] == 0 && top[now + 1][6] != right) {
q.push(now + 1);
flag[now + 1] = flag[now] * -1;
}
}
q.pop();
}
}
int answer = 0;
if (top[0][0] == 1) answer += 1;
if (top[1][0] == 1) answer += 2;
if (top[2][0] == 1) answer += 4;
if (top[3][0] == 1) answer += 8;
cout << answer;
return 0;
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- ํฑ๋์ ์ํ๋ vector ์ ์ค์ฒฉ์ ์ด์ฉํ๊ณ , ์ ํฑ๋์๊ฒ ์ํฅ์ ์ฃผ๋ ๊ฒ์ queue๋ก ๊ตฌํํ๋ค.
- queue์ ์ฌ์ฉ์ ๋น ์ง์ง ์๋... ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๋ฆฌ์คํธ flag๋ก ๋ฐฉํฅ์ ์ ์ฅํ๋ค.
- ํฑ๋์ ํ์ ์ turn ํจ์๋ฅผ ๋ง๋ค์ด์ ํฑ๋ ๋ฒํธ์ ๋ฐฉํฅ์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๋๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ค.
๐ธ end ๐ธ
- ๋ฌธ์ ์ดํด์ ํ์ด ์๊ฐ์ด ๊ฑฐ์ ๋น์ทํ๊ฒ ๊ฑธ๋ฆฐ ๊ฒ ๊ฐ๋ค.
- ๊ณจ๋ 5 ๋ฌธ์ ์์ง๋ง, vector์ queue์ฌ์ฉ์ผ๋ก ์๊ฐ๋ณด๋ค ์์ฃผ ์ฝ๊ฒ ํ๋ ธ๋ค.
- ์๋ฃํ์ ์ ์ ํํ ๊ฒฝ์ฐ์ธ ๊ฒ ๊ฐ๋ค.
728x90