문제 링크입니다: https://school.programmers.co.kr/learn/courses/30/lessons/160585#
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
아래와 같은 조건을 확인해야 합니다.
1. O와 X의 개수
2. O가 이겼을 때는 O가 X보다 1개 많음
3. X가 이겼을 때는 X가 O와 똑같음
주의: 아래와 같은 경우도 정상 케이스입니다.
["OOO"]
["XOX"]
["OXX"]
O가 (0, 2)에 마지막으로 배치하면 O가 두 줄이지만 정상 케이스입니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
const int SIZE = 3; | |
typedef struct | |
{ | |
int oCnt, xCnt; | |
} State; | |
State getWinnerCnts(vector<string> &board) | |
{ | |
int oCnt = 0; | |
int xCnt = 0; | |
if (board[0][0] != '.' | |
&& board[0][0] == board[1][1] | |
&& board[0][0] == board[2][2]) | |
{ | |
board[0][0] == 'O' ? oCnt++ : xCnt++; | |
} | |
if (board[0][2] != '.' | |
&& board[0][2] == board[1][1] | |
&& board[0][2] == board[2][0]) | |
{ | |
board[0][2] == 'O' ? oCnt++ : xCnt++; | |
} | |
for (int y = 0; y < SIZE; y++) | |
{ | |
if (board[y][0] != '.' | |
&& board[y][0] == board[y][1] | |
&& board[y][0] == board[y][2]) | |
{ | |
board[y][0] == 'O' ? oCnt++ : xCnt++; | |
} | |
} | |
for (int x = 0; x < SIZE; x++) | |
{ | |
if (board[0][x] != '.' | |
&& board[0][x] == board[1][x] | |
&& board[0][x] == board[2][x]) | |
{ | |
board[0][x] == 'O' ? oCnt++ : xCnt++; | |
} | |
} | |
return {oCnt, xCnt}; | |
} | |
State getOXCnts(vector<string> &board) | |
{ | |
int oCnt = 0; | |
int xCnt = 0; | |
for (int y = 0; y < SIZE; y++) | |
{ | |
for (int x = 0; x < SIZE; x++) | |
{ | |
if (board[y][x] == 'O') | |
{ | |
oCnt++; | |
} | |
if (board[y][x] == 'X') | |
{ | |
xCnt++; | |
} | |
} | |
} | |
return {oCnt, xCnt}; | |
} | |
int solution(vector<string> board) { | |
State oXCnts = getOXCnts(board); | |
State winnerOXCnts = getWinnerCnts(board); | |
if (oXCnts.xCnt > oXCnts.oCnt | |
|| oXCnts.oCnt - oXCnts.xCnt >= 2) | |
{ | |
return 0; | |
} | |
if (winnerOXCnts.oCnt == 1 | |
&& oXCnts.oCnt != oXCnts.xCnt + 1) | |
{ | |
return 0; | |
} | |
if (winnerOXCnts.xCnt == 1 | |
&& oXCnts.oCnt != oXCnts.xCnt) | |
{ | |
return 0; | |
} | |
return 1; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영합니다! 질문 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 3월에 태어난 여성 회원 목록 출력하기 (0) | 2023.03.01 |
---|---|
[Programmers] 가격대 별 상품 개수 구하기 (0) | 2023.02.28 |
[Programmers] 혼자 놀기의 달인 (0) | 2023.02.22 |
[Programmers] 년, 월, 성별 별 상품 구매 회원 수 구하기 (0) | 2023.02.21 |
[Programmers] 상품 별 오프라인 매출 구하기 (0) | 2023.02.19 |