Chinaunix首页 | 论坛 | 博客
  • 博客访问: 592627
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类: Python/Ruby

2009-04-06 17:17:04

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];

阅读(902) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-08-22 20:24:01

这段代码真简洁