Sudoku Program(In Python)
时间:2009-04-06 来源:cobrawgl
As i said in my previous post,I am posting the new sudoku program,which is written in python 2.5 programming language.
array=[[0,2,0,0,4,0,0,5,0],[0,0,0,7,2,6,0,0,0],[0,0,7,0,9,0,6,0,0],
[7,0,8,0,5,0,2,0,6],[9,0,0,0,0,0,0,0,4],[5,0,1,0,8,0,7,0,9],
[0,0,6,0,1,0,8,0,0],[0,0,0,8,7,3,0,0,0],[0,7,0,0,6,0,0,4,0]]
def Search(List,value):
return List.__contains__(value)
while(0 in [array[i][j] for i in range(9) for j in range(9)]):
for i in range(1,10):
for j in range(9):
count=0;
for k in range(9):
if(array[j][k] ==0):
A=[array[j][l] for l in range(9)];
B=[array[l][k] for l in range(9)];
C=[array[l][m] for l in range((j-j%3),(j-j%3)+3) for m in range((k-k%3),(k-k%3)+3) ];
if(not Search(A,i) and not Search(B,i) and not Search(C,i)):
count,row,column=count+1,j,k;
if(count==1):
array[row][column]=i;
for element in range(9):
print array[element];
相关阅读 更多 +