Chinaunix首页 | 论坛 | 博客
  • 博客访问: 159352
  • 博文数量: 26
  • 博客积分: 1723
  • 博客等级: 上尉
  • 技术积分: 329
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-04 14:06
文章分类

全部博文(26)

文章存档

2016年(1)

2013年(5)

2011年(3)

2009年(6)

2008年(11)

分类: Python/Ruby

2013-07-01 15:29:40


根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复 

python code :

点击(此处)折叠或打开

  1. # -*- coding:utf-8 -*-

  2. # temp_list = range(10)
  3. # temp_list.pop(0)

  4. biaozheng=[1,2,3,4,5,6,7,8,9]
  5. MAX_LINE=MAX_RAW=9
  6. OFFSET = 3

  7. def check_line(line,count):
  8.     for i in xrange(MAX_LINE):
  9.         if shudu[line][i] == count:
  10.             return False

  11.     return True


  12. def check_raw(raw,count):
  13.     for i in xrange(MAX_RAW):
  14.         if shudu[i][raw] == count:
  15.             return False

  16.     return True

  17. def check_block(line,raw,count):
  18.     temp_line = line/OFFSET
  19.     temp_raw = raw/OFFSET

  20.     for i in xrange(OFFSET):
  21.         for j in xrange(OFFSET):
  22.             if shudu[temp_line*3+i][temp_raw*3+j] == count:
  23.                 return False

  24.     return True


  25. def check_all():
  26.     for i in xrange(MAX_LINE):
  27.         for j in xrange(MAX_RAW):
  28.             if shudu[i][j] == 0:
  29.                 return True

  30.     return False


  31. def find_only(temp_x,temp_y):
  32.     temp_list=[]

  33.     # for i in xrange(MAX_LINE):
  34.     #     temp_list.append(0)

  35.     # print 'init_temp_list= '
  36.     # for i in xrange(MAX_LINE):
  37.     #     print temp_list[i],

  38.     # for i in xrange(MAX_LINE):
  39.     #     if check_line(temp_x,biaozheng[i]):
  40.     #          if check_raw(temp_y,biaozheng[i]):
  41.     #              if check_block(temp_x,temp_y,biaozheng[i]):    
  42.     #                 temp_list.append(biaozheng[i])

  43.     for i in xrange(MAX_LINE):
  44.         if check_line(temp_x,biaozheng[i]) and check_raw(temp_y,biaozheng[i]) and check_block(temp_x,temp_y,biaozheng[i]) :
  45.             temp_list.append(biaozheng[i])

  46.     if len(temp_list) == 1:
  47.         # print 'temp_list isnot []'
  48.         # for j in len(temp_list):
  49.         #     print temp_list[j],
  50.         return temp_list[0]
  51.     # else:
  52.     #     print 'temp_list is []'    
  53.     # for i in len(temp_list):
  54.     #     print temp_list[i]        

  55.     # print

  56.     # if len(temp_list) == 1:
  57.     #     # for i in len(temp_list):
  58.     #     print temp_list[0]        
  59.     #     return temp_list[0]


  60.     return 0            


  61. if __name__ == '__main__':
  62.     shudu = []
  63.     for i in xrange(9):
  64.         shudu.append([])

  65.     for i in xrange(9):    
  66.         for j in xrange(9):
  67.             shudu[i].append(0)


  68.     shudu[1][2]=shudu[3][0]= shudu[8][7]=shudu[4][8]=shudu[5][4]=1
  69.     shudu[3][4]=shudu[6][7]=shudu[5][0]=2
  70.     shudu[0][5]=shudu[2][1]=shudu[4][3]=3
  71.     shudu[2][3]=shudu[3][8]=shudu[4][0]=shudu[7][2]=4
  72.     shudu[0][3]=shudu[1][6]=shudu[8][5]=5
  73.     shudu[0][1]=shudu[5][6]=shudu[6][5]=6
  74.     shudu[7][8]=shudu[8][3]=7
  75.     shudu[3][2]=shudu[6][1]=shudu[7][6]=shudu[8][4]=8
  76.     shudu[0][4]=shudu[1][0]=shudu[2][7]=shudu[4][5]=shudu[5][8]=9

  77.     # for i in xrange(9):
  78.     #     for j in xrange(9):
  79.     #         print shudu[i][j],
  80.     #     print


  81.     while check_all():
  82.         for i in xrange(MAX_LINE):
  83.             for j in xrange(MAX_RAW):
  84.                 if shudu[i][j]==0 :
  85.                     temp_num = find_only(i,j)
  86.                     if temp_num != 0:
  87.                         shudu[i][j] = temp_num
  88.                         print i,j,temp_num
  89.                         # break


  90.     # find_only(0,0)


  91.     for i in xrange(9):
  92.         for j in xrange(9):
  93.             print shudu[i][j],
  94.         print

阅读(3839) | 评论(0) | 转发(0) |
0

上一篇:IP地址范围详解

下一篇:数字游戏 2

给主人留下些什么吧!~~