๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
ํฑ๋๋ฐํด๋ฅผ ์๊ณ ํน์ ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ์ํค๋ ๊ธฐ๋ฅ๊ณผ ์ ์ ํฑ๋๋ฐํด์ ๋ง๋ฌผ๋ฆฐ ์ด๋นจ์ ์์ ๊ทน์ด ์๋ก ๋ค๋ฅผ ๊ฒฝ์ฐ, ํด๋น ํฑ๋๋ฐํด๋ค ๋ํ ์ฌ๊ท์ ์ผ๋ก ํ์ ์์ผ์ฃผ๋ ๊ฒ์ด ํต์ฌ์ ๋๋ค. ์ด๊ฑธ ์ด๋ป๊ฒ ๊ตฌํํ๋์ง ํ ๋จ๊ณ์ฉ ์ฐจ๋ก๋๋ก ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ 1๋ฒ๋ถํฐ 4๋ฒ ํฑ๋๋ฐํด์ ๊ฐ ์ด๋นจ์ ๊ทน ์ ๋ณด๋ฅผ ๋ฐฐ์ด์๋ค๊ฐ ์ ์ฅํด์ค๋๋ค.
#define GEARWHELL_COUNT 4
vector<string> gearWheels(GEARWHELL_COUNT + 1);
for (int i = 1; i <= GEARWHELL_COUNT; i++)
cin >> gearWheels[i];
๊ทธ ํ, ํฑ๋๋ฐํด๋ฅผ ํ์ ์ํค๋ ๊ฐ์์ธ K ๊ฐ์ ์ ๋ ฅ ๋ฐ์ ๋ฃจํ๋ฅผ ๋๋ฉด์, ํ์ ์ํฌ ํฑ๋๋ฐํด ๋ฒํธ์ ๋ฐฉํฅ์ ์ ๋ณด๋ฅผ ๋ฐ์์ค๋๋ค. ์ด ์ ๋ณด๋ฅผ ํ ๋๋ก, ์ ์ ํฑ๋๋ฐํด๋ค์ ์ฌ๊ท์ ์ผ๋ก ํ์ ์์ผ ๋๊ฐ ๊ฑด๋ฐ, ์ฃผ์ํ ์ ์ด ์์ต๋๋ค. ํ์ ์ ์งํํ ํฑ๋๋ฐํด๋ ๋ค์ ํ์ ์ํค๋ฉด ์ ๋ฉ๋๋ค.
์ด๋ฅผ ๊ตฌ๋ณํด์ฃผ๊ธฐ ์ํด, ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ณ๋์ bool ๋ฐฐ์ด์ ์์ฑํ์์ต๋๋ค.
int K;
cin >> K;
while (K--)
{
int gearWheelNumber, rotationDirection;
cin >> gearWheelNumber >> rotationDirection;
vector<bool> visited(GEARWHELL_COUNT + 1, false);
RotateGearWheels(gearWheels, visited, gearWheelNumber, rotationDirection);
}
์ด ๋ฌธ์ ์ ํต์ฌ ๊ตฌํ ํจ์์ธ RotateGearWheels()๋ฅผ ๋ณด๊ฒ ์ต๋๋ค. ํฑ๋๋ฐํด์ ๊ฐ ์ด๋นจ์ 12์ ๋ฐฉํฅ์ ์๋ ์ด๋นจ์ ๊ธฐ์ค์ผ๋ก ์๊ณ ๋ฐฉํฅ์ผ๋ก 0, 1, 2, ..., 7 ์์๋๋ก ์ธ๋ฑ์ค๋ฅผ ๋งค๊ฒจ๋์์ต๋๋ค.
#define LEFT_INTERLOCKING_TOOTH 6
#define RIGHT_INTERLOCKING_TOOTH 2
#define GEARWHELL_COUNT 4
#define DIRECTION_COUNT 8
void RotateGearWheels(vector<string>& gearWheels, vector<bool>& visited, const int gearId, const int direction)
{
visited[gearId] = true;
int leftGearWheelId = gearId - 1, rightGearWheelId = gearId + 1;
if (1 <= leftGearWheelId and !visited[leftGearWheelId])
{
if (gearWheels[leftGearWheelId][RIGHT_INTERLOCKING_TOOTH] != gearWheels[gearId][LEFT_INTERLOCKING_TOOTH])
RotateGearWheels(gearWheels, visited, leftGearWheelId, -direction);
}
if (rightGearWheelId <= 4 and !visited[rightGearWheelId])
{
if (gearWheels[rightGearWheelId][LEFT_INTERLOCKING_TOOTH] != gearWheels[gearId][RIGHT_INTERLOCKING_TOOTH])
RotateGearWheels(gearWheels, visited, rightGearWheelId, -direction);
}
// ...
}
ํฌ๊ฒ ์ด๋ ค์ด ์ฝ๋๊ฐ ์๋๊ธฐ์, ์ฝ๊ฒ ์ดํดํ์ค ์ ์์ ๊ฑฐ๋ผ ์๊ฐํฉ๋๋ค. 1)ํ์ฌ ๋ฐฉ๋ฌธํ ํฑ๋๋ฐํด๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ ํ, 2)์ ์ ํฑ๋๋ฐํด์ ๋ฒํธ๋ฅผ ๊ตฌํด, 3)ํ์ ์์ผ์ผ ํ๋ ์ํฉ์ผ ๊ฒฝ์ฐ์๋ง ํด๋น ํฑ๋๋ฐํด๋ค์ ํ์ ์ํค๋ ๋ก์ง์ ๋๋ค.
ํ์ ์์ผ์ผ ํ๋ ์ํฉ์ด๋ ์ผ์ชฝ ํฑ๋๋ฐํด์ 2๋ฒ ์ด๋นจ๊ณผ ๋ด ํฑ๋๋ฐํด์ 6๋ฒ ์ด๋นจ์ ๊ทน์ด ๋ค๋ฅผ ๊ฒฝ์ฐ, ์ค๋ฅธ์ชฝ ํฑ๋๋ฐํด์ 6๋ฒ ์ด๋นจ๊ณผ ๋ด ํฑ๋๋ฐํด์ 2๋ฒ ์ด๋นจ์ ๊ทน์ด ์๋ก ๋ค๋ฅผ ๊ฒฝ์ฐ์ ๋๋ค. ์ด ๋, ํ์ ํด์ผ ํ๋ ์ ํฑ๋๋ฐํด๋ ํ์ฌ ๋ด ํฑ๋๋ฐํด๊ฐ ํ์ ํด์ผ ํ๋ ๋ฐฉํฅ์ ๋ฐ๋ ๋ฐฉํฅ์ด๋ฏ๋ก, ์์(-) ๋ถํธ๋ฅผ ๋ถ์ฌ์ ํจ์๋ฅผ ์คํ์์ผ์ฃผ์์ต๋๋ค.
์ ๋ก์ง์ ํตํด ๋ ์ด์ ์ ์์ ํ์ ํ ํฑ๋๋ฐํด๊ฐ ์๋ ํฑ๋๋ฐํด๊น์ง ์์ ๊ฒฝ์ฐ, ์ด์ ํ์ ์ ์์ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
#define LEFT_INTERLOCKING_TOOTH 6
#define RIGHT_INTERLOCKING_TOOTH 2
#define GEARWHELL_COUNT 4
#define DIRECTION_COUNT 8
void RotateGearWheels(vector<string>& gearWheels, vector<bool>& visited, const int gearId, const int direction)
{
// ...
int rotationCount = 0, toothId = 0;
char previousMagneticPole = gearWheels[gearId][toothId];
char temp;
while (rotationCount < DIRECTION_COUNT)
{
int nextToothId = (toothId + DIRECTION_COUNT + direction) % DIRECTION_COUNT;
temp = gearWheels[gearId][nextToothId];
gearWheels[gearId][nextToothId] = previousMagneticPole;
previousMagneticPole = temp;
toothId = nextToothId;
rotationCount++;
}
}
K ๋ฒ ํ์ ์ ๋ค ํ๋ค๋ฉด, 1๋ฒ ~ 4๋ฒ ํฑ๋๋ฐํด์ 0๋ฒ(12์ ๋ฐฉํฅ)์ด S๊ทน์ผ ๊ฒฝ์ฐ์๋ง ์ ์๋ฅผ ๋งค๊ฒจ์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๊ฒ ์ต๋๋ค.
#define S '1'
int CalculatedPoint(const vector<string>& gearWheels)
{
int totalPoint = 0;
for (int i = 1; i <= 4; i++)
if (gearWheels[i][0] == S)
totalPoint += 1 << (i - 1);
return totalPoint;
}
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define S '1'
#define LEFT_INTERLOCKING_TOOTH 6
#define RIGHT_INTERLOCKING_TOOTH 2
#define GEARWHELL_COUNT 4
#define DIRECTION_COUNT 8
int CalculatedPoint(const vector<string>& gearWheels)
{
int totalPoint = 0;
for (int i = 1; i <= 4; i++)
if (gearWheels[i][0] == S)
totalPoint += 1 << (i - 1);
return totalPoint;
}
void RotateGearWheels(vector<string>& gearWheels, vector<bool>& visited, const int gearId, const int direction)
{
visited[gearId] = true;
int leftGearWheelId = gearId - 1, rightGearWheelId = gearId + 1;
if (1 <= leftGearWheelId and !visited[leftGearWheelId])
{
if (gearWheels[leftGearWheelId][RIGHT_INTERLOCKING_TOOTH] != gearWheels[gearId][LEFT_INTERLOCKING_TOOTH])
RotateGearWheels(gearWheels, visited, leftGearWheelId, -direction);
}
if (rightGearWheelId <= 4 and !visited[rightGearWheelId])
{
if (gearWheels[rightGearWheelId][LEFT_INTERLOCKING_TOOTH] != gearWheels[gearId][RIGHT_INTERLOCKING_TOOTH])
RotateGearWheels(gearWheels, visited, rightGearWheelId, -direction);
}
int rotationCount = 0, toothId = 0;
char previousMagneticPole = gearWheels[gearId][toothId];
char temp;
while (rotationCount < DIRECTION_COUNT)
{
int nextToothId = (toothId + DIRECTION_COUNT + direction) % DIRECTION_COUNT;
temp = gearWheels[gearId][nextToothId];
gearWheels[gearId][nextToothId] = previousMagneticPole;
previousMagneticPole = temp;
toothId = nextToothId;
rotationCount++;
}
}
int main()
{
FAST_IO
vector<string> gearWheels(GEARWHELL_COUNT + 1);
for (int i = 1; i <= GEARWHELL_COUNT; i++)
cin >> gearWheels[i];
int K;
cin >> K;
while (K--)
{
int gearWheelNumber, rotationDirection;
cin >> gearWheelNumber >> rotationDirection;
vector<bool> visited(GEARWHELL_COUNT + 1, false);
RotateGearWheels(gearWheels, visited, gearWheelNumber, rotationDirection);
}
cout << CalculatedPoint(gearWheels);
return 0;
}
'๐คAlgorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 20056๋ฒ | ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ (Java) (1) | 2024.07.25 |
---|---|
[BOJ] 12904๋ฒ | A์ B (C++) (0) | 2024.03.28 |
[BOJ] 2636๋ฒ | ์น์ฆ (C++) (1) | 2024.03.22 |
[BOJ] 14499๋ฒ | ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ (C++) (1) | 2024.03.17 |
[BOJ] 14725๋ฒ | ๊ฐ๋ฏธ๊ตด (C++) (1) | 2024.02.15 |