본문 바로가기
알고리즘/백준 문제

7682 - 틱택토

by HDobby 2023. 8. 4.

https://www.acmicpc.net/problem/7682

 

7682번: 틱택토

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 줄은 9개의 문자를 포함하며, 'X', 'O', '.' 중 하나이다. '.'은 빈칸을 의미하며, 9개의 문자는 게임판에서 제일 윗 줄 왼쪽부터의 순서이다. 입

www.acmicpc.net

요건

  • 조건이 굉장히 많습니다...
  • x가 o보다 1개 혹은 0개가 더 많은지
  • 둘다 false일때 x와 o의 합이 9개가 아닌지 (게임이 종료 됐을 때의 가능한지 여부)
  • o가 성공 했으나 x가 더 많은 경우
  • x가 성공 했으나 o와 갯수가 동일한 경우

예제

XOXOXOXO.
end




INVALID

코드

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int chk[8][3][2]={
    {
        {0,0}, {1,0}, {2,0}
    },
    {
        {0,1}, {1,1}, {2,1}
    },
    {
        {0,2}, {1,2}, {2,2}
    },
    {
        {0,0}, {0,1}, {0,2}
    },
    {
        {1,0}, {1,1}, {1,2}
    },
    {
        {2,0}, {2,1}, {2,2}
    },
    {
        {0,0}, {1,1}, {2,2}
    },
    {
        {0,2}, {1,1}, {2,0}
    }
};

bool calc(string str){
    int xcnt=0, ocnt=0;

    for(int i=0;i<9;++i){
        if(str[i] == 'O')
            ++ocnt;
        else if(str[i] == 'X')
            ++xcnt;
    }

    int dif = xcnt - ocnt;
    if(dif < 0 || dif > 1)
        return false;

    bool xflag = false, oflag = false;
    for(int m=0;m<8;++m){
        if(
            str[chk[m][0][0]*3+chk[m][0][1]] == str[chk[m][1][0]*3+chk[m][1][1]] &&
            str[chk[m][1][0]*3+chk[m][1][1]] == str[chk[m][2][0]*3+chk[m][2][1]]
        ){
            if(str[chk[m][0][0]*3+chk[m][0][1]] == 'X')
                xflag=true;

            else if(str[chk[m][0][0]*3+chk[m][0][1]] == 'O')
                oflag=true;
        }
    }

    if((xflag && oflag) || ((!xflag && !oflag) && (xcnt+ocnt != 9)))
        return false;

    if((oflag && dif == 1) || (xflag && dif != 1))
        return false;
    return true;
}

void solution(){
    string input;

    while(true){
        cin>>input;

        if(input.compare("end") == 0)
            return;

        if(calc(input))
            cout<<"valid"<<'\n';
        else
            cout<<"invalid"<<'\n';
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solution();

    return 0;
}

728x90

'알고리즘 > 백준 문제' 카테고리의 다른 글

1863 - 스카이라인 쉬운거  (0) 2023.08.06
2138 - 전구와 스위치  (0) 2023.08.06
2467 - 용액  (0) 2023.08.02
5972 - 택배 배송  (0) 2023.07.26
1943 - 동전 분배  (0) 2023.07.25

댓글