study
혼자서 하는 틱택토[프로그래머스]
Date: 2026-06-07 16:36
Update: 2026-06-07 16:43
혼자서 하는 틱택토[프로그래머스]
접근법:
- O와 X 의 갯수를 센다.
- O가 이겼는지 X가 이겼는지 센다.
- 순서는 항상 O가 선공이니깐 O 부터 계산한다.
- X의 갯수가 O 보다 많으면 잘못된 틱택토
- O의 갯수가 X + 1 보다 많으면 잘못된 틱택토
- O가 이겼는데 O와 X의 수가 같으면 잘못된 틱택토
- X가 이겼는데 O의 갯수가 X + 1 보다 많으면 잘못된 틱택토
#include <string>
#include <vector>
#include <queue>
using namespace std;
bool isWin(char c, const vector<string>& board)
{
for(int i = 0; i < 3; ++i)
{
if(board[i][0] == c && board[i][1] == c && board[i][2] == c) return true;
if(board[0][i] == c && board[1][i] == c && board[2][i] == c) return true;
}
if(board[0][0] == c && board[1][1] == c && board[2][2] == c) return true;
if(board[2][0] == c && board[1][1] == c && board[0][2] == c) return true;
return false;
}
int solution(vector<string> board) {
bool isOWin = isWin('O', board);
bool isXWin = isWin('X', board);
int countO = 0;
int countX = 0;
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
if(board[i][j] == 'O') countO++;
else if(board[i][j] == 'X') countX++;
}
}
if(countX > countO || countO > countX + 1) return 0;
if(isOWin && countO == countX) return 0;
if(isXWin && countO == countX + 1) return 0;
return 1;
}
.gif)