[프로그래머스 C++] 혼자서 하는 틱택토

 

https://school.programmers.co.kr/learn/courses/30/lessons/160585

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

 

해결전략

 

구현

 


 

 

정답코드

 

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int solution(vector<string> b) {
    int answer = 1;

    int SumBingGo_O = 0; // O 전체 빙고 수
    int SumBingGo_X = 0; // X 전체 빙고 수

    int black = 0; // O 개수
    int white = 0; // X 개수

    vector<int> ColumnBingGo_O(3, 0); // O 세로 빙고
    vector<int> ColumnBingGo_X(3, 0); // X 세로 빙고

    for (int y = 0; y < 3; y++)
    {
        int RowBingGo_X = 0;
        int RowBingGo_O = 0;
        for (int x = 0; x < 3; x++)
        {
            if (b[y][x] == 'O') black++;
            else if (b[y][x] == 'X') white++;

            if (x < 2 && b[y][x] == b[y][x + 1] && b[y][x] == 'O') RowBingGo_O++;
            if (x < 2 && b[y][x] == b[y][x + 1] && b[y][x] == 'X') RowBingGo_X++;
            if (y < 2 && b[y][x] == b[y + 1][x] && b[y][x] == 'O') ColumnBingGo_O[x]++;
            if (y < 2 && b[y][x] == b[y + 1][x] && b[y][x] == 'X') ColumnBingGo_X[x]++;
        }
		
        // 가로 빙고
        if (RowBingGo_O == 2) SumBingGo_O++;
        if (RowBingGo_X == 2) SumBingGo_X++;
    }
	
    // 세로 빙고
    for (int i = 0; i < 3; i++) { 
        if (ColumnBingGo_O[i] == 2) SumBingGo_O++;
        if (ColumnBingGo_X[i] == 2) SumBingGo_X++;
    }

    // 대각선 빙고
    if (b[0][0] == b[1][1] && b[1][1] == b[2][2] && b[1][1] == 'O') SumBingGo_O++;
    if (b[0][2] == b[1][1] && b[1][1] == b[2][0] && b[1][1] == 'O') SumBingGo_O++;
    if (b[0][0] == b[1][1] && b[1][1] == b[2][2] && b[1][1] == 'X') SumBingGo_X++;
    if (b[0][2] == b[1][1] && b[1][1] == b[2][0] && b[1][1] == 'X') SumBingGo_X++;

    // 후공인 X가 더 많은 경우, 선공인 O가 1개 초과 더 많은 경우
    if (black < white || black > white + 1) {
        return 0;
    }
        
    if (SumBingGo_O >= 1){
        if (SumBingGo_X >= 1) return 0;
        if (black <= white) return 0;
    }

    if (SumBingGo_X >= 1){
        if (SumBingGo_O >= 1) return 0;
        if (black != white) return 0;
    }
    
    return answer;
}

int main(){
    vector<string> testcase1 = { "O.X", ".O.", "..X" };
    vector<string> testcase2 = { "OOO", "...", "XXX" };
    vector<string> testcase3 = { "...", ".X.", "..." };
    vector<string> testcase4 = { "...", "...", "..." };
    vector<string> testcase5 = { "XXO", ".OX", "O.." };
    vector<string> testcase6 = { "OOO", "XOX", "OXX" };
    vector<string> testcase7 = { "XOX", "OXO", "XOX" };

    cout << solution(testcase1) << "\n"; // 1
    cout << solution(testcase2) << "\n"; // 0
    cout << solution(testcase3) << "\n"; // 0
    cout << solution(testcase4) << "\n"; // 1
    cout << solution(testcase5) << "\n"; // 0
    cout << solution(testcase6) << "\n"; // 1
    cout << solution(testcase7) << "\n"; // 0

    return 0;
}

 

 


 

참고자료

 

2022.11.09 - [⭐ Programming/자료구조와 알고리즘] - [알고리즘] 동적계획법 - Tic-Tae-Toe