Problem:


パズル

たろう君は 9 &times; 9 のマス目に 1〜9 の数字を配置するパズルで遊んでいます。このパズルでは以下の規則で数字を配置しなければいけません。

   
同じ列に 1 つの数字はちょうど 1 回だけ現われる

   
同じ行に 1 つの数字はちょうど 1 回だけ現われる

   
二重線で区切られた 3 &times; 3 の各範囲には、1 つの数字はちょうど 1 回だけ現われる

例えば、下の図 1 はそのような規則を満たした配置の一つです。しかしたろう君は、図 2 のようによく規則に反した配置を作ってしまいます。左端の列に「2」が 2 回現われて、「1」が 1 回も現われず、左から2 番目の列に「1」が 2 回現われて、「2」が 1 回も現われていません。

図 1 
図 2 

たろう君を助けるために、数字の配置を読み込んで、その配置が規則を満たしているかどうかを調べ、規則に反していたらその場所を出力するプログラムを作成してください。3 つの規則に照らして誤っている (2 回以上現れている) 数字の前には * (半角アスタリスク)を、誤っていない数字の前には空白を表示してください。

Input

複数のデータセットが与えられます。最初の行にデータセットの数 n (n ≤ 20) が与えられます。各データセットとして、パズルの状態を示す 1 行 9 文字、9 行からなる数字列が与えられます。

Output

各データセットについて以下を出力してください。

与えられた数字と * (半角アスタリスク)と空白。誤っている数字の前には *、誤っていない数字の前には半角空白を付加する。

データセットの間に 1 行の空行を入れてください。

Sample Input

2
2 1 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8
2 1 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8

Output for the Sample Input

*2*1 3 4 5 6 7 8 9
 4 5 6 7 8 9 1 2 3
 7 8 9 1 2 3 4 5 6
*2 3 4 5 6 7 8 9 1
 5 6 7 8 9 1 2 3 4
 8 9 1 2 3 4 5 6 7
 3 4 5 6 7 8 9 1 2
 6 7 8 9 1 2 3 4 5
 9*1 2 3 4 5 6 7 8

*2*1 3 4 5 6 7 8 9
 4 5 6 7 8 9 1 2 3
 7 8 9 1 2 3 4 5 6
*2 3 4 5 6 7 8 9 1
 5 6 7 8 9 1 2 3 4
 8 9 1 2 3 4 5 6 7
 3 4 5 6 7 8 9 1 2
 6 7 8 9 1 2 3 4 5
 9*1 2 3 4 5 6 7 8



Sample input:
2
2 1 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8
2 1 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8


Sample output:
*2*1 3 4 5 6 7 8 9
 4 5 6 7 8 9 1 2 3
 7 8 9 1 2 3 4 5 6
*2 3 4 5 6 7 8 9 1
 5 6 7 8 9 1 2 3 4
 8 9 1 2 3 4 5 6 7
 3 4 5 6 7 8 9 1 2
 6 7 8 9 1 2 3 4 5
 9*1 2 3 4 5 6 7 8

*2*1 3 4 5 6 7 8 9
 4 5 6 7 8 9 1 2 3
 7 8 9 1 2 3 4 5 6
*2 3 4 5 6 7 8 9 1
 5 6 7 8 9 1 2 3 4
 8 9 1 2 3 4 5 6 7
 3 4 5 6 7 8 9 1 2
 6 7 8 9 1 2 3 4 5
 9*1 2 3 4 5 6 7 8

Submission status: Wrong Answer

Solution code:
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  M=[]
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3]] for x in R for y in R]
  for y in N:
    A=M[y]
    for x in N:
      A1=M2[x/3+y/3*3]
      a=A[x]
      if A.count(a)>1 or M1[x].count(a)>1 or A1.count(a)>1: F[y][x]="*"
  for i in N:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Wrong Answer

Solution code:
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  M=[]
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3]] for y in R for x in R]
  for y in N:
    A=M[y]
    for x in N:
      A1=M2[x/3+y/3*3]
      a=A[x]
      if A.count(a)>1 or M1[x].count(a)>1 or A1.count(a)>1: F[y][x]="*"
  for i in N:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Wrong Answer

Solution code:
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  M=[]
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    A=M[y]
    for x in N:
      A1=M2[x/3+y/3*3]
      a=A[x]
      print x,y,a,A1
      if A.count(a)>1 or M1[x].count(a)>1 or A1.count(a)>1: F[y][x]="*"
  for i in N:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Wrong Answer

Solution code:
n = input()

for i in range(0, n):
	puzzle = [list(raw_input().replace(" ", "")) for _ in range(0, 9)]
	
	for i in range(0, 9):
		for j in range(0, 9):
			judge = True
			for k in range(0, 9):
				if puzzle[i][j] == puzzle[i][k] and j != k and puzzle[i][j][:1] != "*":
					puzzle[i][j] = "*" + puzzle[i][j];
					puzzle[i][k] = "*" + puzzle[i][k];
				if puzzle[i][j] == puzzle[k][j] and i != k and puzzle[i][j][:1] != "*":
					puzzle[i][j] = "*" + puzzle[i][j];
					puzzle[k][j] = "*" + puzzle[k][j];
					
	for i in range(0, 9):
		for j in range(0, 9):
			if puzzle[i][j][:1] == "*":
				print puzzle[i][j],
			else:
				print " " + puzzle[i][j], 
		print
	print

Submission status: Wrong Answer

Solution code:
n = input()

for i in range(0, n):
	puzzle = [list(raw_input().replace(" ", "")) for _ in range(0, 9)]
	
	for i in range(0, 9):
		for j in range(0, 9):
			judge = True
			for k in range(0, 9):
				if puzzle[i][j] == puzzle[i][k] and j != k and puzzle[i][j][:1] != "*":
					puzzle[i][j] = "*" + puzzle[i][j];
					puzzle[i][k] = "*" + puzzle[i][k];
				if puzzle[i][j] == puzzle[k][j] and i != k and puzzle[i][j][:1] != "*":
					puzzle[i][j] = "*" + puzzle[i][j];
					puzzle[k][j] = "*" + puzzle[k][j];
			if i==j==0 or i==j==3 or i==j==6 or i==0 and j == 3 or i==0 and j==6 or i==3 and j==0 or i==3 and j==6 or i==6 and j==0 or i==6 and j==3:
				for k in range(i, i + 3):
					for h in range(j, j + 3):
						if puzzle[i][j] == puzzle[k][h] and puzzle[i][j][:1] != "*" and j != h and i != k:
							puzzle[i][j] = "*" + puzzle[i][j];
							puzzle[k][h] = "*" + puzzle[k][h];
					
	for i in range(0, 9):
		for j in range(0, 9):
			if puzzle[i][j][:1] == "*":
				print puzzle[i][j],
			else:
				print " " + puzzle[i][j], 
		print
	print

Submission status: Wrong Answer

Solution code:
n=input()
for i in range(n):
    L=[raw_input().split() for i in range(9)]
    for i in range(9):
        S1,S2=set(),set()
        for j in range(9):
            if L[j][i] not in S1:
                S1.add(L[j][i])
            else:
                S2.add(L[j][i])
        for j in range(9):
            if (L[j][i] in S2) and (L[j][i][0]!="*"):
                L[j][i]="*"+L[j][i]
            elif L[j][i][0]!="*" and L[j][i][0]!=" ":
                L[j][i]=" "+L[j][i]


    for j in range(9):
        S1,S2=set(),set()
        for i in range(9):
            if L[j][i] not in S1:
                S1.add(L[j][i])
            else:
                S2.add(L[j][i])
        for i in range(9):
            if (L[j][i] in S2) and (L[j][i][0]!="*"):
                L[j][i]="*"+L[j][i]
            elif L[j][i][0]!="*" and L[j][i][0]!=" ":
                L[j][i]=" "+L[j][i]

    for i in range(0,9,3):
        for j in range(0,9,3):
            S1,S2=set(),set()
            for k in range(3):
                for l in range(3):
                    if L[i+k][j+l] not in S1:
                        S1.add(L[i+k][j+l])
                    else:
                        S2.add(L[i+k][j+l])
            for k in range(3):
                for l in range(3):
                    if (L[i+k][j+l] in S2) and (L[i+k][j+l][0]!="*"):
                        L[i+k][j+l]="*"+L[i+k][j+l]
                    elif (L[i+k][j+l][0]!=" ") and (L[i+k][j+l][0]!="*"):
                        L[i+k][j+l]=" "+L[i+k][j+l]

    for i in range(9):
        print "".join(L[i])

Submission status: Wrong Answer

Solution code:
n=input()
for f in range(n):
    L=[raw_input().split() for i in range(9)]
    for i in range(9):
        S1,S2=set(),set()
        for j in range(9):
            if L[j][i] not in S1:
                S1.add(L[j][i])
            else:
                S2.add(L[j][i])
        for j in range(9):
            if (L[j][i] in S2) and (L[j][i][0]!="*"):
                L[j][i]="*"+L[j][i]
            elif L[j][i][0]!="*" and L[j][i][0]!=" ":
                L[j][i]=" "+L[j][i]


    for j in range(9):
        S1,S2=set(),set()
        for i in range(9):
            if L[j][i] not in S1:
                S1.add(L[j][i])
            else:
                S2.add(L[j][i])
        for i in range(9):
            if (L[j][i] in S2) and (L[j][i][0]!="*"):
                L[j][i]="*"+L[j][i]
            elif L[j][i][0]!="*" and L[j][i][0]!=" ":
                L[j][i]=" "+L[j][i]

    for i in range(0,9,3):
        for j in range(0,9,3):
            S1,S2=set(),set()
            for k in range(3):
                for l in range(3):
                    if L[i+k][j+l] not in S1:
                        S1.add(L[i+k][j+l])
                    else:
                        S2.add(L[i+k][j+l])
            for k in range(3):
                for l in range(3):
                    if (L[i+k][j+l] in S2) and (L[i+k][j+l][0]!="*"):
                        L[i+k][j+l]="*"+L[i+k][j+l]
                    elif (L[i+k][j+l][0]!=" ") and (L[i+k][j+l][0]!="*"):
                        L[i+k][j+l]=" "+L[i+k][j+l]

    for i in range(9):
        print "".join(L[i])

Submission status: Wrong Answer

Solution code:
for r in range(input()):
	if r > 0: print
	A = [raw_input().split() for i in range(9)]
	P = [[" "]*9 for i in range(9)]
	for h in range(9):
		for w in range(9):
			if A[h].count(A[h][w]) > 1 or [A[hi][w] for hi in range(9)].count(A[h][w]) > 1:
				P[h][w] = "*"
	for h in range(9):
		print "".join([P[h][w]+A[h][w] for w in range(9)])

Submission status: Wrong Answer

Solution code:
for r in range(input()):
	if r > 0: print
	A  = [raw_input().split() for i in range(9)]
	RA = [[A[h][w] for h in range(9)] for w in range(9)] 
	A9 = [[A[h][w:w+3]+A[h+1][w:w+3]+A[h+2][w:w+3] for w in range(0,9,3)] for h in range(0,9,3)]
	P = [[" "]*9 for i in range(9)]
	for h in range(9):
		for w in range(9):
			if P[h][w] != "*":
				if A[h].count(A[h][w]) > 1:
					P[h][w] == "*"
				elif RA[w].count(A[h][w]) > 1:
					P[h][w] = "*"
				elif A9[h/3][w/3].count(A[h][w]) > 1:
					P[h][w] = "*"
	for h in range(9):
		print "".join([P[h][w]+A[h][w] for w in range(9)])

Submission status: Accepted

Solution code:
R=range(9)
def f1(M):
  global F
  for y in R:
    A=M[y]
    if len(set(A))==9: continue
    for x in R:
      if A.count(A[x])>=2: F[y][x]="*"
  return
def f2(M):
  global F
  for x in R:
    A=[M[y][x] for y in R]
    if len(set(A))==9: continue
    for y in R:
      if A.count(A[y])>=2: F[y][x]="*"
  return
def f3(M):
  global F
  for i in R:
    x=i%3*3
    y=i/3*3
    A=M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3]
    if len(set(A))==9: continue
    for j in R:
      if A.count(A[j])>=2: F[y+j/3][x+j%3]="*"
  return

n=input()
while n:
  M=[]
  F=[[" " for _ in [0]*9] for _ in [0]*9]
  M=[map(int,raw_input().split()) for _ in [0]*9]
  f1(M)
  f2(M)
  f3(M)
  for i in R:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
R=range(9)
def f1(M):
  global F
  for y in R:
    A=M[y]
    for x in R:
      if A.count(A[x])>=2: F[y][x]="*"
  return
def f2(M):
  global F
  for x in R:
    A=[M[y][x] for y in R]
    for y in R:
      if A.count(A[y])>=2: F[y][x]="*"
  return
def f3(M):
  global F
  for i in R:
    x=i%3*3
    y=i/3*3
    A=M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3]
    for j in R:
      if A.count(A[j])>=2: F[y+j/3][x+j%3]="*"
  return

n=input()
while n:
  M=[]
  F=[[" " for _ in [0]*9] for _ in [0]*9]
  M=[map(int,raw_input().split()) for _ in [0]*9]
  f1(M)
  f2(M)
  f3(M)
  for i in R:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
R=range(9)
Z=[0]*9
def f(M):
  global F
  def f1(i):
    if A.count(A[i])>=2: F[y][x]="*"
    return
  for y in R:
    A=M[y]
    for x in R: f1(x)
  for x in R:
    A=[M[y][x] for y in R]
    for y in R: f1(y)
  for i in R:
    x0=i%3*3
    y0=i/3*3
    A=[]
    for j in [0,1,2]: A+=M[y0+j][x0:x0+3]
    for j in R:
      y=y0+j/3
      x=x0+j%3
      f1(j)
  return

n=input()
while n:
  M=[]
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  f(M)
  for i in R:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  M=[]
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    A=M[y]
    for x in N:
      A1=M2[x/3+y/3*3]
      a=A[x]
      if A.count(a)>1 or M1[x].count(a)>1 or A1.count(a)>1: F[y][x]="*"
  for i in N:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    A=M[y]
    p0=y/3*3
    for x in N:
      p=p0+x/3
      a=A[x]
      if A.count(a)>1 or M1[x].count(a)>1 or M2[p].count(a)>1: F[y][x]="*"
  for i in N:
    s=zip(F[i],map(str,M[i]))
    print "".join([a+b for a,b in s])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    A=M[y]
    p0=y/3*3
    for x in N:
      p=p0+x/3
      a=A[x]
      if A.count(a)>1 or M1[x].count(a)>1 or M2[p].count(a)>1: F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,A))])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
def f(A): return A.count(a)>1
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    A=M[y]
    p0=y/3*3
    for x in N:
      p=p0+x/3
      a=A[x]
      if any(map(f,[A,M1[x],M2[p]])): F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,A))])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
def f(A): return A.count(a)>1
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    p0=y/3*3
    for x in N:
      a=M[y][x]
      if any(map(f,[M[y],M1[x],M2[p0+x/3]])): F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,M[y]))])
  if n>1: print
  n-=1

Submission status: Accepted

Solution code:
def f1():
    for r in range(9):
        for c in range(9):
            flag = Map[r][c]
            i_l = []
            for i in range(9):
                if  Map[r][i] == flag:
                    i_l.append(i)
            if len(i_l) != 1:
                for i in i_l:
                    if not (r, i) in res:
                        res.append((r, i))
                        
def f2():
    for c in range(9):
        for r in range(9):
            flag = Map[r][c]
            i_l = []
            for i in range(9):
                if  Map[i][c] == flag:
                    i_l.append(i)
            if len(i_l) != 1:
                for i in i_l:
                    if not (i, c) in res:
                        res.append((i, c))

def f3():
    for r in range(0, 9, 3):
        for c in range(0, 9, 3):
            for _r in range(3):
                for _c in range(3):
                    f_3(r, c, Map[r+_r][c+_c])
                   
def f_3(r, c, flag):
    i_l = []
    for _r in range(3):
        for _c in range(3):
            if Map[r+_r][c+_c] == flag:
                i_l.append((r+_r, c+_c))
    if len(i_l) != 1:
        for t in i_l:
            if not t in res:
                res.append(t)
                
def f4():
    for r in range(9):
        for c in range(9):
            if (r, c) in res:
                Map[r][c] = '*'+Map[r][c]
            else:
                Map[r][c] = ' '+Map[r][c]
    
    for line in Map:
        print ''.join(line)
    if n != N-1:
        print
        
if __name__ == '__main__':
    N = input()
    for n in range(N):
        Map = [raw_input().split() for i in range(9)]
        res = []
        f1()
        f2()
        f3()
        f4()

Submission status: Accepted

Solution code:
t = input()
while t:
    b = [map(int, raw_input().split()) for i in xrange(9)]
    for i in xrange(9):
        pnt = ""
        for j in xrange(9):
            error = False
            # tate
            for k in xrange(9):
                if k==i:
                    continue
                if b[k][j] == b[i][j]:
                    error = True
                    break
            # yoko
            if not error:
                for k in xrange(9):
                    if k==j:
                        continue
                    if b[i][k] == b[i][j]:
                        error = True
                        break
            # 3 kake 3
            if not error:
                ix = j//3*3; iy = i//3*3
                for y in xrange(3):
                    if error:
                        break
                    for x in xrange(3):
                        if ix+x==j and iy+y==i:
                            continue
                        if b[iy+y][ix+x] == b[i][j]:
                            error = True
                            break

            pnt += "*%d"%b[i][j] if error else " %d"%b[i][j]
        print pnt
    if t>1:
        print
    t -= 1

Submission status: Accepted

Solution code:
t = input()
while t:
    b = [map(int, raw_input().split()) for i in xrange(9)]
    for i in xrange(9):
        pnt = ""
        for j in xrange(9):
            error = False
            # tate
            for k in xrange(9):
                if k==i:
                    continue
                if b[k][j] == b[i][j]:
                    error = True
                    break
            # yoko
            for k in xrange(9):
                if k==j:
                    continue
                if b[i][k] == b[i][j]:
                    error = True
                    break
            # 3 kake 3
            ix = j//3*3; iy = i//3*3
            for y in xrange(iy, iy+3):
                if error:
                    break
                for x in xrange(ix, ix+3):
                    if x==j and y==i:
                        continue
                    if b[y][x] == b[i][j]:
                        error = True
                        break

            pnt += "*%d"%b[i][j] if error else " %d"%b[i][j]
        print pnt
    if t>1:
        print
    t -= 1

Submission status: Accepted

Solution code:
t = input()
while t:
    b = [map(int, raw_input().split()) for i in xrange(9)]
    for i in xrange(9):
        pnt = ""
        for j in xrange(9):
            error = False
            ix = j//3*3; iy = i//3*3
            for k in xrange(9):
                # tate
                if k!=i and b[k][j] == b[i][j]:
                    error = True
                    break
                # yoko
                if k!=j and b[i][k] == b[i][j]:
                    error = True
                    break
                # 3 kake 3
                y = iy + k/3; x = ix + k%3
                if (x != j or y != i) and b[y][x] == b[i][j]:
                    error = True
                    break

            pnt += "*%d"%b[i][j] if error else " %d"%b[i][j]
        print pnt
    if t>1:
        print
    t -= 1

Submission status: Accepted

Solution code:
# Aizu Problem 00126: Puzzle
#
import sys, math, os, copy

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def check_puzzle(puzzle):
    marker = [[' ' for _ in range(9)] for __ in range(9)]
    # check rows:
    for r in range(9):
        row = puzzle[r]
        for k in range(1, 10):
            if row.count(k) != 1:
                for idx in [j for j in range(9) if row[j] == k]:
                    marker[r][idx] = '*'
    # check columns:
    for c in range(9):
        col = [puzzle[r][c] for r in range(9)]
        for k in range(1, 10):
            if col.count(k) != 1:
                for idx in [j for j in range(9) if col[j] == k]:
                    marker[idx][c] = '*'
    # check sub-squares:
    for rs in range(3):
        for rc in range(3):
            square = puzzle[3*rs][3*rc:3*rc+3] + puzzle[3*rs+1][3*rc:3*rc+3] + \
                     puzzle[3*rs+2][3*rc:3*rc+3]
            for k in range(1, 10):
                if square.count(k) != 1:
                    for idx in [j for j in range(9) if square[j] == k]:
                        ridx = 3 * rs + idx // 3
                        cidx = 3 * rc + idx % 3
                        marker[ridx][cidx] = '*'
            
    return marker


first = True
N = int(input())
for n in range(N):
    puzzle = [[int(_) for _ in input().split()] for __ in range(9)]
    marker = check_puzzle(puzzle)
    if first:
        first = False
    else:
        print()
    for k in range(9):
        out = ''.join([marker[k][j] + str(puzzle[k][j]) for j in range(9)])
        print(out)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0126

"""
import sys
from sys import stdin
from collections import defaultdict
input = stdin.readline




def solve(data):
    status = [[' '] * 9 for _ in range(9)]

    # ?¨???????????????§??????
    for y, row in enumerate(data):
        freq = defaultdict(int)
        for i, n in enumerate(row):
            if n in freq:
                freq[n].append(i)
            else:
                freq[n] = [i]
        for k, v in freq.items():
            if len(v) > 1:
                for ind in v:
                    status[y][ind] = '*'

    # ?????????????????§??????
    for x in range(9):
        freq = defaultdict(int)
        for y in range(9):
            n = data[y][x]
            if n in freq:
                freq[n].append(y)
            else:
                freq[n] = [y]
        for k, v in freq.items():
            if len(v) > 1:
                for ind in v:
                    status[ind][x] = '*'

    # 3x3????????§??????
    for y in range(0, 9, 3):
        for x in range(0, 9, 3):
            freq = defaultdict(int)
            for yy in range(3):
                for xx in range(3):
                    n = data[y+yy][x+xx]
                    if n in freq:
                        freq[n].append((x+xx, y+yy))
                    else:
                        freq[n] = [(x+xx, y+yy)]

            for k, v in freq.items():
                if len(v) > 1:
                    for xx, yy in v:
                        status[yy][xx] = '*'

    for y in range(9):
        for x in range(9):
            print('{}{}'.format(status[y][x], data[y][x]), end='')
        print()


def main(args):
    n = int(input())
    for i in range(n):
        data = [[int(x) for x in input().split()] for _ in range(9)]
        result = solve(data)
        if i != n-1:
            print()



if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
from collections import Counter

CounterKey = ("1", "2", "3", "4", "5", "6", "7", "8", "9")

inputCount = int(input())

for _ in range(inputCount):
    puzzle = []
    mark = []

    if _ != 0:
        print()

    for lp in range(9):
        puzzle.append([item for item in input().split(" ")])
        mark.append([item for item in " " * 9])

    # 横
    for rowIndex, row in enumerate(puzzle):
        counter = Counter(row)
        needNum = [num for num in CounterKey if 1 < counter[num]]

        if not needNum:
            continue

        for colIndex, item in enumerate(row):
            if item in needNum:
                mark[rowIndex][colIndex] = "*"

    # 縦
    for colIndex, col in enumerate(
            zip(puzzle[0], puzzle[1], puzzle[2], puzzle[3], puzzle[4], puzzle[5], puzzle[6], puzzle[7], puzzle[8])):
        counter = Counter(col)
        needNum = [num for num in CounterKey if 1 < counter[num]]

        if not needNum:
            continue

        for rowIndex, item in enumerate(col):
            if item in needNum:
                mark[rowIndex][colIndex] = "*"

    # 四角
    for lp in range(3):
        block = []

        for blockCount, col in enumerate(zip(puzzle[lp * 3], puzzle[lp * 3 + 1], puzzle[lp * 3 + 2])):
            block.extend(col)

            if blockCount % 3 - 2 == 0:

                counter = Counter(block)
                needNum = [num for num in CounterKey if 1 < counter[num]]

                if needNum:

                    for index, item in enumerate(block):
                        if item in needNum:
                            rowIndex, colIndex = lp * 3 + index % 3, blockCount - 2 + index // 3
                            mark[rowIndex][colIndex] = "*"

                block = []

    # 出力
    for p, m in zip(puzzle, mark):
        output = []

        for num, state in zip(p, m):
            output.append(state)
            output.append(num)

        print("".join(output))

Submission status: Accepted

Solution code:
import math

def isPutNum(board, x, y, n):
    for i in range(9):
        if board[x][i] == n:
            return False
    for i in range(9):
        if board[i][y] == n:
            return False
    xx = math.floor(x / 3)
    yy = math.floor(y / 3)
    for i in range(3):
        for j in range(3):
            if board[xx*3 + i][yy*3 + j] == n:
                return False
    return True

N = int(input())
table = [[0 for i in range(9)] for j in range(9)]
for l in range(N):
    if l > 0:
        print("")

    for i in range(9):
        table[i] = [int(j) for j in input().split()]

    for i in range(9):
        for j in range(9):
            tmp = table[i][j]
            table[i][j] = 0
            if isPutNum(table, i, j, tmp):
                print(" ", end="")
            else:
                print("*", end="")
            table[i][j] = tmp
            print(table[i][j], end="")
        print("")

Submission status: Accepted

Solution code:
def trans(mp):
  ret = [[None] * 9 for _ in range(9)]
  for x in range(9):
    for y in range(9):
      ret[x][y] = mp[y][x]

  return ret

def fix(mp):
  fix_lst = []
  mp2 = trans(mp)
  for i in range(9):
    for j in range(9):
      if mp[i].count(mp[i][j]) > 1:
        fix_lst.append((i, j))
      if mp2[i].count(mp2[i][j]) > 1:
        fix_lst.append((j, i))

  for ulx in (0, 3, 6):
    for uly in (0, 3, 6):
      tmp = []
      for dx in (0, 1, 2):
        for dy in (0, 1, 2):
          tmp.append(mp[ulx + dx][uly + dy])

      for dx in (0, 1, 2):
        for dy in (0, 1, 2):
          if tmp.count(mp[ulx + dx][uly + dy]) > 1:
            fix_lst.append((ulx + dx, uly + dy))
  
  fix_lst = list(set(fix_lst))
  for x, y in fix_lst:
    mp[x][y] = "*"  + mp[x][y]

  return mp

def _rj(c):
  return c.rjust(2)

def print_mp(mp):
  for line in mp:
    print("".join(map(_rj, line)))

n = int(input())
for i in range(n):
  if i != 0:
    print()
  mp = [input().split() for _ in range(9)]
  mp = fix(mp)
  print_mp(mp)

Submission status: Accepted

Solution code:
from __future__ import print_function
from sys import stdin, stdout

try:
    xrange
except NameError:
    pass
else:
    range = xrange

def solve():
    table = []
    for _ in range(9):
        table.append(stdin.readline().split())
    
    check_table = []
    for _ in range(9):
        check_table.append([False] * 9)
    
    # horizontal check
    for i in range(9):
        a = []
        for j in range(9):
            a.append(table[i][j])
        for j in range(9):
            n = table[i][j]
            if a.count(n) > 1:
                check_table[i][j] = True
    
    # vertical check
    for j in range(9):
        a = []
        for i in range(9):
            a.append(table[i][j])
        for i in range(9):
            n = table[i][j]
            if a.count(n) > 1:
                check_table[i][j] = True
    
    # 9x9 check
    for i in range(0, 9, 3):
        for j in range(0, 9, 3):
            a = []
            for di in range(3):
                for dj in range(3):
                    a.append(table[i + di][j + dj])
            for di in range(3):
                for dj in range(3):
                    n = table[i + di][j + dj]
                    if a.count(n) > 1:
                        check_table[i + di][j + dj] = True
    
    for i in range(9):
        for j in range(9):
            if check_table[i][j]:
                stdout.write('*')
            else:
                stdout.write(' ')
            stdout.write(table[i][j])
        stdout.write('\n')

if __name__ == '__main__':
    N = int(stdin.readline())
    for i in range(N):
        if i > 0:
            stdout.write('\n')
        solve()

Submission status: Accepted

Solution code:
def judge(L,y,x):
    T=[0]*10
    for i in range(y,y+3):
        for j in range(x,x+3):
            T[L[i][j]]+=1
    for t in range(1,10):
        if T[t]>1:
            for i in range(y,y+3):
                for j in range(x,x+3):
                    if L[i][j]==t:
                        P[i][j]="*"

def judgeRow(L,y):
    T=[0]*10
    for i in range(9):
        T[L[y][i]]+=1
    for i in range(1,10):
        if T[i]>1:
            for j in range(9):
                if L[y][j]==i:
                    P[y][j]="*"

def judgeCul(L,x):
    T=[0]*10
    for i in range(9):
        T[L[i][x]]+=1
    for i in range(1,10):
        if T[i]>1:
            for j in range(9):
                if L[j][x]==i:
                    P[j][x]="*"

n=input()
for dt in range(n):
    L=[map(int,raw_input().split()) for i in range(9)]
    P=[[" "]*9 for i in range(9)]
    for i in range(0,9,3):
        for j in range(0,9,3):
            judge(L,i,j)

    for i in range(9):
        judgeRow(L,i)
    for i in range(9):
        judgeCul(L,i)
    for i,j in zip(P,L):
        s=""
        for k,l in zip(i,j):
            s+=k+str(l)
        print s
    if dt!=n-1:
        print

Submission status: Accepted

Solution code:
field =[]
mark = []

def check_row(n):
    counts = [field[n].count(i) for i in xrange(10)]
    for x,v in enumerate(field[n]):
        if counts[v] > 1:
            put(x,n)

def check_col(n):
    t_field = zip(*field)
    counts = [t_field[n].count(i) for i in xrange(10)]
    for y,v in enumerate(t_field[n]):
        if counts[v] > 1:
            put(n,y)

def check_sq(n):
    ltx,lty = 3*(n%3), (n/3)*3
    tmp = reduce(lambda a,b:a+b,[field[y][ltx:ltx+3]
                   for y in xrange(lty,lty+3)])
    counts = [tmp.count(i) for i in xrange(10)]
    for x in xrange(ltx,ltx+3):
        for y in xrange(lty,lty+3):
            if counts[field[y][x]] > 1:
                put(x,y)

def put(x,y):
    global mark
    mark[y][x] = "*"

N = input()
while True:
    N -= 1
    field = [map(int,raw_input().split()) for _ in xrange(9)]
    mark = [[" "]*9 for _ in xrange(9)]
    for i in xrange(9):
        check_row(i)
        check_col(i)
        check_sq(i)
    for m,f in zip(mark,field):
        print "".join(mm+str(ff) for mm,ff in zip(m,f))
    if N <= 0:
        break
    print

Submission status: Accepted

Solution code:
for r in range(input()):
	if r > 0: print
	A  = [raw_input().split() for i in range(9)]
	RA = [[A[h][w] for h in range(9)] for w in range(9)] 
	A9 = [[A[h][w:w+3]+A[h+1][w:w+3]+A[h+2][w:w+3] for w in range(0,9,3)] for h in range(0,9,3)]
	P = [[" "]*9 for i in range(9)]
	for h in range(9):
		for w in range(9):
			if P[h][w] != "*":
				if A[h].count(A[h][w]) > 1:
					P[h][w] = "*"
				elif RA[w].count(A[h][w]) > 1:
					P[h][w] = "*"
				elif A9[h/3][w/3].count(A[h][w]) > 1:
					P[h][w] = "*"
	for h in range(9):
		print "".join([P[h][w]+A[h][w] for w in range(9)])