码迷,mamicode.com
首页 > 其他好文 > 详细

Validate a sudo puzzle

时间:2015-04-18 13:03:41      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

这个解法告诉我们读题要精确,且例证了多维数组的重要性。

# Sudoku [http://en.wikipedia.org/wiki/Sudoku]

# A valid sudoku square satisfies these
# two properties:

# 1. Each column of the square contains each of the whole numbers from 1 to n exactly once.

# 2. Each row of the square contains each of the whole numbers from 1 to n exactly once.

# You may assume the the input is square and contains at least one row and column.

def check_sudoku(m):
  n = len(m) # set the size of the matrix
  for x in range(n):
  row,col = [],[] # use lists so I can use "in"
  for y in range(n):
    row.append(m[x][y]) # build row and column list
    col.append(m[y][x])
  for i in range(1,n+1): # count from 1 to n
    if (i) not in col or (i) not in row:
    return False # Fail if integer is missing
  return True

 

Validate a sudo puzzle

标签:

原文地址:http://www.cnblogs.com/7070roro/p/4437175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!