# 2019 IUSB Programming Competition
# Round 2 Problem 2
# Find a winning move for Tic-Tac-Toe
# Solution by Dana Vrajitoru
# Uses Python 3.6.2
# Checks if the player has a horizontal winning move
def checkRows(table, player):
for i in range(3):
for j in range(3):
if table[i][j] == 0 and table[i][(j+1)%3] == player and \
table[i][(j+2)%3] == player:
return (i, j)
return (-1, -1)
# Checks if the player has a vertical winning move
def checkColumns(table, player):
for i in range(3):
for j in range(3):
if table[i][j] == 0 and table[(i+1)%3][j] == player and \
table[(i+2)%3][j] == player:
return (i, j)
return (-1, -1)
# Checks if the player has a winning move on the first diagonal
def checkFirstDiagonal(table, player):
for i in range(3):
if table[i][i] == 0 and table[(i+1)%3][(i+1)%3] == player and \
table[(i+2)%3][(i+2)%3] == player:
return (i, i)
return (-1, -1)
# Checks if the player has a winning move on the second diagonal
def checkSecDiagonal(table, player):
for i in range(3):
if table[i][2-i] == 0 and table[(i+1)%3][2-(i+1)%3] == player and \
table[(i+2)%3][2-(i+2)%3] == player:
return (i, 2-i)
return (-1, -1)
def checkPrint(tupple):
if tupple[0] != -1:
print("%d %d" %(tupple[0], tupple[1]))
return True
else:
return False
# Testing the function
if __name__ == '__main__':
print("Enter the table and player:")
table = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
str = input().split(" ")
for j in range(3):
table[i][j] = int(str[j])
player = int(input())
if not checkPrint(checkRows(table, player)):
if not checkPrint(checkColumns(table, player)):
if not checkPrint(checkFirstDiagonal(table, player)):
if not checkPrint(checkSecDiagonal(table, player)):
print("-1 -1")