Created
June 19, 2012 10:37
-
-
Save erning/2953457 to your computer and use it in GitHub Desktop.
判断数独的棋盘是否符合规则
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int validate(int *pieces) { | |
int sc[] = {0,0,0,0,0,0,0,0,0}; | |
int sr[] = {0,0,0,0,0,0,0,0,0}; | |
int sb[] = {0,0,0,0,0,0,0,0,0}; | |
int n; | |
for (n = 0; n < 81; n++) { | |
int x = pieces[n]; | |
if (x == 0) continue; | |
if (x < 1 || x > 9) return 0; | |
int c = n % 9; int r = n / 9; int b = c / 3 + r / 3 * 3; | |
x = 1 << (x - 1); | |
if (sc[c] & x || sr[r] & x || sb[b] & x) { | |
return 0; | |
} | |
sc[c] |= x; sr[r] |= x; sb[b] |= x; | |
} | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function validate($pieces) { | |
$sc = array(0,0,0,0,0,0,0,0,0); | |
$sr = array(0,0,0,0,0,0,0,0,0); | |
$sb = array(0,0,0,0,0,0,0,0,0); | |
for ($n = 0; $n < 81; $n++) { | |
$x = $pieces[$n]; | |
if ($x == 0) { | |
continue; | |
} | |
if ($x < 1 || $x > 9) { | |
return false; | |
} | |
$c = $n % 9; | |
$r = (int)($n / 9); | |
$b = (int)($c / 3) + (int)($r / 3) * 3; | |
$x = 1 << ($x - 1); | |
if ($sc[$c] & $x || $sr[$r] & $x || $sb[$b] & $x) { | |
return false; | |
} | |
$sc[$c] |= $x; $sr[$r] |= $x; $sb[$b] |= $x; | |
} | |
return True; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate(pieces): | |
if len(pieces) != 81: | |
return False | |
col = lambda n : n % 9 | |
row = lambda n : n / 9 | |
blk = lambda n : col(n) / 3 + row(n) / 3 * 3 | |
sc = [set() for i in xrange(9)] | |
sr = [set() for i in xrange(9)] | |
sb = [set() for i in xrange(9)] | |
n = -1 | |
for x in pieces: | |
n += 1 | |
if x == 0: | |
continue | |
if x < 1 or x > 9: | |
return False | |
c, r, b = col(n), row(n), blk(n) | |
if x in sc[c] or x in sr[r] or x in sb[b]: | |
return False | |
sc[c].add(x) | |
sr[r].add(x) | |
sb[b].add(x) | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
输入参数是包含81个0..9元素的数组