STUDY/Algorithm
[백준] 7682 틱택토 python
sinawi95
2022. 4. 8. 11:54
728x90
https://www.acmicpc.net/problem/7682
7682번: 틱택토
틱택토 게임은 두 명의 사람이 번갈아가며 말을 놓는 게임이다. 게임판은 3×3 격자판이며, 처음에는 비어 있다. 두 사람은 각각 X 또는 O 말을 번갈아가며 놓는데, 반드시 첫 번째 사람이 X를 놓고
www.acmicpc.net
구현문제이다.
# import sys; input = sys.stdin.readline
def check_line(string, player):
ret = False
if player == string[0] == string[1] == string[2]\
or player == string[3] == string[4] == string[5]\
or player == string[6] == string[7] == string[8]\
or player == string[0] == string[3] == string[6]\
or player == string[1] == string[4] == string[7]\
or player == string[2] == string[5] == string[8]\
or player == string[0] == string[4] == string[8]\
or player == string[2] == string[4] == string[6]:
ret = True
return ret
def check_string(string):
win_x, win_o = check_line(string, 'X'), check_line(string, 'O')
num_x, num_o, num_dot = string.count('X'), string.count('O'), string.count('.')
if (win_x and not win_o and num_x == num_o + 1)\
or (not win_x and win_o and num_x == num_o)\
or (not win_x and not win_o and num_x == 5 and num_o == 4):
return "valid"
return "invalid"
def main():
answer = []
while True:
string = input().rstrip()
if string == "end":
break
answer.append(check_string(string))
print(*answer, sep='\n')
if __name__ == "__main__":
main()