2023. 7. 5. 20:05γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π¨π»νμ΄ κ³Όμ
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;
}
'Coding Test > 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 |