2024. 3. 17. 16:35ใCoding Test/BOJ
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ผ์ข ์ ๊ณต๊ฐ์ง๊ฐ๋ฅ๋ ฅ(?)์ ํ์๋ก ํ๋ ๊ตฌํ ๋ฌธ์ ์ ๋๋ค. ์ด๊ธฐ์ ์ฃผ์ด์ง ์ฃผ์ฌ์ ์ํ์์ ๋, ์, ๋จ, ๋ถ ๋ฐฉํฅ์ผ๋ก ๊ตฌ๋ฅด๋ฉด ๊ทธ์ ๋ฐ๋ผ ์ฃผ์ฌ์ ๋ฉด์ ์ ํ ์ซ์๋ฅผ ์ ์ ๋ฆฌํ๋ ์ ์ฐจ๊ฐ ํ์ํ์ฃ . ๊ทธ๋์ ์ ๋ 6๊ฐ์ ์์๋ฅผ ๊ฐ์ง๋ 1์ฐจ์ ๋ฐฐ์ด์๋ค๊ฐ ์ฃผ์ฌ์์ ๊ฐ ๋ฉด์ ์ ํ ์ซ์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ์ต๋๋ค.
enum DIRECTION
{
TOP,
RIGHT,
LEFT,
FORWARD,
BACK,
DOWN,
};
vector<int> SurfaceOfDice(6);
์ด์ ์ฃผ์ด์ง ๋ช ๋ น์ด ์ด๋ ๋ฐฉํฅ์ ๋ฐ๋ผ ์ฃผ์ฌ์์ ๋ค์ ์์น๋ฅผ ๊ตฌํด์ฃผ๊ณ , ํด๋น ์์น๊ฐ ๋งต ์์ผ ๊ฒฝ์ฐ์๋ง ๋ค์์ฒ๋ผ ์ฃผ์ฌ์ ๋ฉด์ ์ซ์ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธ ํด์ค๋๋ค.
- ๋(RIGHT) : 1)์ค๋ฅธ๋ฉด์ด ์๋ซ๋ฉด, 2)์ ๋ฉด์ด ์ค๋ฅธ๋ฉด, 3) ์ผ๋ฉด์ด ์ ๋ฉด, 4) ์๋ซ๋ฉด์ด ์ผ๋ฉด
- ์(LEFT) : 1)์ผ๋ฉด์ด ์๋ซ๋ฉด, 2)์ ๋ฉด์ด ์ผ๋ฉด, 3)์ค๋ฅธ๋ฉด์ด ์ ๋ฉด, 4) ์๋ซ๋ฉด์ด ์ค๋ฅธ๋ฉด
- ๋ถ(FORWARD) : 1)์ ๋ฉด์ด ์๋ซ๋ฉด, 2) ์ ๋ฉด์ด ์ ๋ฉด, 3) ํ๋ฉด์ด ์ ๋ฉด, 4) ์๋ซ๋ฉด์ด ํ๋ฉด
- ๋จ(BACK) : 1)ํ๋ฉด์ด ์๋ซ๋ฉด, 2) ์ ๋ฉด์ด ํ๋ฉด, 3)์ ๋ฉด์ด ์ ๋ฉด, 4)์๋ซ๋ฉด์ด ์ ๋ฉด
bool Move(Point& dicePosition, const int directionIndex, const int maxRow, const int maxColumn)
{
int nextDiceRow = dicePosition.first + Directions[directionIndex].first;
int nextDiceColumn = dicePosition.second + Directions[directionIndex].second;
if (nextDiceRow < 0 or nextDiceRow >= maxRow or nextDiceColumn < 0 or nextDiceColumn >= maxColumn)
return false;
switch(directionIndex)
{
case RIGHT:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[RIGHT];
SurfaceOfDice[RIGHT] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[LEFT];
SurfaceOfDice[LEFT] = temp;
break;
}
case LEFT:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[LEFT];
SurfaceOfDice[LEFT] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[RIGHT];
SurfaceOfDice[RIGHT] = temp;
break;
}
case FORWARD:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[FORWARD];
SurfaceOfDice[FORWARD] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[BACK];
SurfaceOfDice[BACK] = temp;
break;
}
case BACK:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[BACK];
SurfaceOfDice[BACK] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[FORWARD];
SurfaceOfDice[FORWARD] = temp;
break;
}
}
dicePosition.first = nextDiceRow;
dicePosition.second = nextDiceColumn;
return true;
}
๋ง์ง๋ง์ผ๋ก ์ด๋์ ์๋ฃํ๋ค๋ฉด, ํด๋น ๋งต ๋ฐ๋ฅ์ ์ ํ ์ซ์์ ๋ฐ๋ผ ๋ก์ง์ ์ฒ๋ฆฌํด์ค๋๋ค.
- ์ด๋ํ ๋งต ์นธ์ด 0์ผ ๊ฒฝ์ฐ : ์ฃผ์ฌ์์ ์๋ซ๋ฉด ์ซ์๋ฅผ ์ด๋ํ ๋งต ์นธ์ ๋ณต์ฌ
- ์ด๋ํ ๋งต ์นธ์ด 0์ด ์๋ ๊ฒฝ์ฐ : ๋งต ์นธ์ ์ซ์๋ฅผ ์ฃผ์ฌ์์ ์๋ซ๋ฉด์ ๋ณต์ฌํ๊ณ , ํด๋น ๋งต ์นธ์ 0์ผ๋ก ์ค์
void Copy(vector<vector<int>>& map, int row, int column)
{
if (map[row][column] == 0)
{
map[row][column] = SurfaceOfDice[DOWN];
return;
}
SurfaceOfDice[DOWN] = map[row][column];
map[row][column] = 0;
}
์ด๋ ๊ฒ ํจ์ผ๋ก์จ, ๋ฌธ์ ๋ฅผ ํ ์ ์์์ต๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <iostream>
#include <vector>
using namespace std;
using Point = pair<int, int>;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
vector<Point> Directions { {}, {0, 1}, {0, -1}, {-1, 0}, {1, 0} }; // DUMMY, ๋, ์, ๋ถ, ๋จ
vector<int> SurfaceOfDice(6);
enum DIRECTION
{
TOP,
RIGHT,
LEFT,
FORWARD,
BACK,
DOWN,
};
bool Move(Point& dicePosition, const int directionIndex, const int maxRow, const int maxColumn)
{
int nextDiceRow = dicePosition.first + Directions[directionIndex].first;
int nextDiceColumn = dicePosition.second + Directions[directionIndex].second;
if (nextDiceRow < 0 or nextDiceRow >= maxRow or nextDiceColumn < 0 or nextDiceColumn >= maxColumn)
return false;
switch(directionIndex)
{
case RIGHT:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[RIGHT];
SurfaceOfDice[RIGHT] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[LEFT];
SurfaceOfDice[LEFT] = temp;
break;
}
case LEFT:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[LEFT];
SurfaceOfDice[LEFT] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[RIGHT];
SurfaceOfDice[RIGHT] = temp;
break;
}
case FORWARD:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[FORWARD];
SurfaceOfDice[FORWARD] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[BACK];
SurfaceOfDice[BACK] = temp;
break;
}
case BACK:
{
int temp = SurfaceOfDice[DOWN];
SurfaceOfDice[DOWN] = SurfaceOfDice[BACK];
SurfaceOfDice[BACK] = SurfaceOfDice[TOP];
SurfaceOfDice[TOP] = SurfaceOfDice[FORWARD];
SurfaceOfDice[FORWARD] = temp;
break;
}
}
dicePosition.first = nextDiceRow;
dicePosition.second = nextDiceColumn;
return true;
}
void Copy(vector<vector<int>>& map, int row, int column)
{
if (map[row][column] == 0)
{
map[row][column] = SurfaceOfDice[DOWN];
return;
}
SurfaceOfDice[DOWN] = map[row][column];
map[row][column] = 0;
}
int main()
{
FAST_IO
// 1. ์
๋ ฅ ๋ฐ๊ธฐ
int maxRow, maxColumn, numOfInstruction, directionIndex;
Point dicePosition;
cin >> maxRow >> maxColumn >> dicePosition.first >> dicePosition.second >> numOfInstruction;
vector<vector<int>> map(maxRow, vector<int>(maxColumn));
for (int row = 0; row < maxRow; row++)
for (int column = 0; column < maxColumn; column++)
cin >> map[row][column];
// 2. ๋ช
๋ น์ด ์ํ
while (numOfInstruction--)
{
cin >> directionIndex;
if (Move(dicePosition, directionIndex, maxRow, maxColumn))
{
cout << SurfaceOfDice[TOP] << "\n";
Copy(map, dicePosition.first, dicePosition.second);
}
}
return 0;
}
'Coding Test > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 14891๋ฒ | ํฑ๋๋ฐํด (C++) (0) | 2024.03.24 |
---|---|
[BOJ] 2636๋ฒ | ์น์ฆ (C++) (1) | 2024.03.22 |
[BOJ] 14725๋ฒ | ๊ฐ๋ฏธ๊ตด (C++) (1) | 2024.02.15 |
[BOJ] 1644๋ฒ | ์์์ ์ฐ์ํฉ (C++) (1) | 2024.02.07 |
[BOJ] 13904๋ฒ | ๊ณผ์ (C++) (1) | 2024.02.06 |