๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
3 X 3 ๋งต์์ ๋จผ์ ๊ฐ๋ก ํน์ ์ธ๋ก ํน์ ๋๊ฐ์ 3๊ฐ์ ๋ผ์ธ์ ๋ง๋๋ ์ฌ๋์ด ์ด๊ธฐ๋ ๊ฒ์์ ๋๋ค. ํ์ง๋ง, ์ด๊ฒผ๋๊ฐ ์ก๋๊ฐ๋ฅผ ๋ณด๋ ๊ฒ์ด ์๋๋ผ, ์ฌ๋ฐ๋ฅด๊ฒ ๊ฒ์์ ์งํํ๋๊ฐ๋ฅผ ๋ณด๋ ๊ฒ์ด๋ฏ๋ก ๋ค์๊ณผ ๊ฐ์ ์ฌํญ๋ค์ ์ฒดํฌํด์ผ ํฉ๋๋ค.
- ๋ฒ๊ฐ์ ๊ฐ๋ฉด์ ์งํํด์ผ ํ๋๋ฐ, ์ด๋ ํ ์ชฝ์ด 2๋ฒ ์ฐ์ ์งํํ ๊ฒฝ์ฐ ( | 'O'์ ๊ฐ์ - 'X'์ ๊ฐ์| > 1)
- ์ ๊ณต์ด ๋จผ์ ๋์ผ ํ๋๋ฐ, ํ๊ณต์ด ๋จผ์ ๋์ ๊ฒฝ์ฐ ( 'O'์ ๊ฐ์ < 'X'์ ๊ฐ์ )
- ๋ ๋ค ์ด๊ฒผ๋ค๊ณ ํ์ ํ ๊ฒฝ์ฐ ( 'O' 3๊ฐ๋ก ํ ๋ผ์ธ์ ๋ง๋ค๊ณ , 'X' 3๊ฐ๋ก๋ ํ ๋ผ์ธ์ ๋ง๋ ๊ฒฝ์ฐ)
- ์ ๊ณต์ด ์ด๊ฒผ์ง๋ง, ํ๊ณต์ด ๊ณ์ ์ด์ด ๋๊ฐ๋ ๊ฒฝ์ฐ ( 'O'์ ๊ฐ์ - 'X'์ ๊ฐ์ <= 0)
- ์ ์์ ์ธ ๊ฒ์์์ ์ ๊ณต์ด ์ด๊ฒผ์ ๊ฒฝ์ฐ์๋ 'O'๊ฐ 'X'๋ณด๋ค 1๊ฐ๊ฐ ๋ ๋ง๋ค.
- ํ๊ณต์ด ์ด๊ฒผ์ง๋ง, ์ ๊ณต์ด ๊ณ์ ์ด์ด๋๊ฐ๋ ๊ฒฝ์ฐ ( 'O'์ ๊ฐ์ == 'X'์ ๊ฐ์)
- ์ ์์ ์ธ ๊ฒ์์์ ํ๊ณต์ด ์ด๊ฒผ์ ๊ฒฝ์ฐ์๋ 'O'์ 'X'์ ๊ฐ์๊ฐ ๊ฐ๋ค.
์์ ๊ฐ์ ์ฌํญ๋ค ์ค ํ๋๋ผ๋ ํด๋น๋๋ค๋ฉด ๊ทธ ๊ฒ์์ ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒ์์ด๋ฏ๋ก 0์ ๋ฆฌํดํด์ผ ํฉ๋๋ค. ์ ์ฌํญ๋ค์ ๋ณด๋ฉด 'O'์ 'X'์ ๊ฐ์๋ก๋ง ํ๋จํ ์ ์๋ ํญ๋ชฉ๋ค๋ ์์ง๋ง, ์ ๊ณต์ด ์ด๊ฒผ๋๊ฐ์ ํ๊ณต์ด ์ด๊ฒผ๋๊ฐ์ ๋ํด์๋ 3๊ฐ์ ์ ์ด ์ด์ด์ง ๋ผ์ธ์ธ์ง๋ฅผ ์ฒดํฌํ๋ ๋ก์ง์ด ํ์ํฉ๋๋ค. ๋ฐ๋ผ์, ๊ทธ ๋ถ๋ถ์ ์์ฑํ ํ ์ ์กฐ๊ฑด๋ค์ ์ ์ฒดํฌํด์ฃผ๋ฉด ๋ฉ๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <vector>
using namespace std;
using Position = pair<int, int>;
const int BOARD_LENGTH = 3;
// ์ข์ฐ, ์ํ, y=-x ๋๊ฐ๋ผ์ธ, y=x ๋๊ฐ๋ผ์ธ
vector<vector<Position>> positionDirections { {{0, -1}, { 0, 1 }}, { {-1, 0}, { 1, 0} }, { {-1, -1}, { 1, 1} }, { {1, -1}, {-1, 1} } };
pair<vector<Position>, vector<Position>> GetPositions(const vector<string>& board) {
vector<Position> firster;
vector<Position> seconder;
for (int row = 0; row < BOARD_LENGTH; row++) {
for (int col = 0; col < BOARD_LENGTH; col++) {
if (board[row][col] == 'O')
firster.emplace_back(row, col);
else if (board[row][col] == 'X')
seconder.emplace_back(row, col);
}
}
return make_pair(firster, seconder);
}
bool IsWin(const vector<string>& board, const vector<Position>& positions, char target) {
for (const auto position : positions) {
int currentRow = position.first;
int currentCol = position.second;
for (const auto& directions : positionDirections) {
bool isWin = true;
for (const auto& direction : directions) {
int nextRow = currentRow + direction.first;
int nextCol = currentCol + direction.second;
if (nextRow < 0 or nextRow >= BOARD_LENGTH or nextCol < 0 or nextCol >= BOARD_LENGTH or board[nextRow][nextCol] != target)
isWin = false;
}
if (isWin)
return true;
}
}
return false;
}
int solution(vector<string> board) {
auto positions = GetPositions(board);
vector<Position> firster = positions.first;
vector<Position> seconder = positions.second;
if (abs((int)firster.size() - (int)seconder.size()) > 1)
return false;
if (firster.size() < seconder.size())
return false;
bool isFirsterWin = IsWin(board, firster, 'O');
bool isSeconderWin = IsWin(board, seconder, 'X');
if (isFirsterWin and isSeconderWin)
return false;
if (isFirsterWin and firster.size() - seconder.size() <= 0)
return false;
if (isSeconderWin and firster.size() != seconder.size())
return false;
return true;
}
'๐คAlgorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ์ด๋ชจํฐ์ฝ ํ ์ธํ์ฌ | C++ (2) | 2023.07.09 |
---|---|
[Programmers] Lv2. ์๊ฒฉ ์์คํ | C++ (0) | 2023.07.06 |
[Programmers] Lv2. ์ซ์ ๋ธ๋ก | C++ (0) | 2023.07.04 |
[Programmers] Lv2. ๋ ์ ์ฌ์ด์ ์ ์ ์ | C++ (0) | 2023.07.03 |
[Programmers] Lv2. ์ฐ๋ฐ์์ด ์ ์ ๋ถ | C++ (0) | 2023.07.01 |