CCC 2019 S3:Arithmetic Square
Case 1: 1 x
Case 2: 2 x
Case 3: 3 x
Case 4: 4 x
We can set row by row , the column by column, you can select d row or columns.
Case 5: 5x
Case 6: 6x:
Case 7: 7x
We can use the following concept:
a a+d a+2d
a+c a+c+d a+c+2d
a+2c a+2c+d a+2c+2d
Case 8: 8x
We can set 8x as the unique number.
Case 9: 9x:
We can set 9x as the same number, I select 0.
list1 = []
def setrow(row):
if list1[row][0] != 'X' and list1[row][1] != 'X' and list1[row][2] == 'X':
list1[row][2] = 2*list1[row][1] - list1[row][0];
return
if list1[row][0] != 'X' and list1[row][2] != 'X' and list1[row][1] == 'X':
list1[row][1] = int((list1[row][2] + list1[row][0])/2)
return
if list1[row][2] != 'X' and list1[row][1] != 'X' and list1[row][0] == 'X':
list1[row][0] = 2*list1[row][1] - list1[row][2]
return
def setcol(col):
if list1[0][col] != 'X' and list1[1][col] != 'X' and list1[2][col] == 'X':
list1[2][col] = 2*list1[1][col] - list1[0][col];
return
if list1[0][col] != 'X' and list1[2][col] != 'X' and list1[1][col] == 'X':
list1[1][col] = int((list1[2][col] + list1[0][col])/2)
return
if list1[2][col] != 'X' and list1[1][col] != 'X' and list1[0][col] == 'X':
list1[0][col] = 2*list1[1][col] - list1[2][col]
return
def setonerow(row, col, d):# set a row with interval=d
if col == 0:
list1[row][1] = list1[row][0] + d
list1[row][2] = list1[row][1] + d
return
if col == 1:
list1[row][0] = list1[row][1] - d
list1[row][2] = list1[row][1] + d
return
if col == 2:
list1[row][1] = list1[row][2] - d
list1[row][0] = list1[row][1] - d
return
def setelement(row, d): #set all rows with the interval=d
for i in range(3):
if i == row:
continue
j = 0
while j < 3:
if list1[i][j] != 'X':
break
j += 1
if j == 0:
list1[i][1] = list1[i][0] + d
list1[i][2] = list1[i][1] + d
elif j == 1:
list1[i][2] = list1[i][1] + d
list1[i][0] = list1[i][1] - d
else:
list1[i][1] = list1[i][2] - d
list1[i][0] = list1[i][1] - d
def printmatrix(): #print the answer
print(list1[0][0], list1[0][1], list1[0][2])
print(list1[1][0], list1[1][1], list1[1][2])
print(list1[2][0], list1[2][1], list1[2][2])
def getx(): #find the amount of X in the square
n = 0
for i in range(3):
for j in range(3):
if list1[i][j] == 'X':
n += 1
return n
def three():
for r in range(3):
setrow(r)
for c in range(3):
setcol(c)
def four():
for r in range(3):
setrow(r)
for c in range(3):
setcol(c)
if getx()==4:
d = 0
i = 0
while i < 3:
if list1[i][0] != 'X' and list1[i][1] != 'X' and list1[i][2] != 'X':
d = list1[i][1] - list1[i][0]
break
i += 1
setelement(i, d)
def five():
four()
if getx() == 4:
d=0
i=0
while i < 3:
if list1[i][0] != 'X' and list1[i][1] != 'X' and list1[i][2] != 'X':
d = list1[i][1] - list1[i][0]
break
i += 1
setelement(i, d)
def six():
three()
if getx() == 6:
if list1[1][1] == 'X':
if list1[0][1] != 'X':
list1[1][1] = list1[0][1]
elif list1[1][0] != 'X':
list1[1][1] = list1[1][0]
elif list1[1][2] != 'X':
list1[1][1] = list1[1][2]
else:
list1[1][0] = list1[1][1]
def seven():
for q in range(3):
setrow(q)
for p in range(3):
setcol(p)
if getx() == 7:
ct = 0
sx = 0
sy = 0
ex = 0
ey = 0
for m in range(3):
for n in range(3):
if list1[m][n] != 'X':
if ct == 0:
sx = m
sy = n
ct += 1
else:
ex = m
ey = n
ct += 1
if ct == 2:
break
if ct == 2:
break
diff = list1[ex][ey] - list1[sx][sy]
xsteps = abs(ex - sx)
ysteps = abs(ey - sy)
if xsteps == ysteps:
setonerow(sx, sy, 0)
setonerow(ex, ey, 0)
else:
if ysteps % 2 != 0:
if diff % 2 == 0:
setonerow(sx, sy, 2)
setonerow(ex, ey, 2)
else:
setonerow(sx, sy, 1)
setonerow(ex, ey, 1)
else:
setonerow(sx, sy, 2)
setonerow(ex, ey, 2)
def setall(aa):
for k in range(3):
for j in range(3):
list1[k][j] = aa
xnum = 0
for i in range(3):
cur = input().rstrip().split(" ")
content = []
for x in cur:
if x == 'X':
content.append(x)
xnum += 1
else:
content.append(int(x))
list1.append(content)
while xnum > 0:
if xnum < 5:
four()
elif xnum == 5:
five()
elif xnum == 6:
six()
elif xnum == 7:
seven()
elif xnum == 8:
for a in range(3):
c = False
for b in range(3):
if list1[a][b] != 'X':
setall(list1[a][b])
c = True
break
if c:
break
else:
setall(0)
xnum = getx()
printmatrix()
コメント