Skip to content

Instantly share code, notes, and snippets.

@wasi0013
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wasi0013/be486c4bf9afe0d98f51 to your computer and use it in GitHub Desktop.
Save wasi0013/be486c4bf9afe0d98f51 to your computer and use it in GitHub Desktop.
Naive sudoku solver
import cProfile
def row_check(i,j): return i//9==j//9
def col_check(i,j): return (i-j)%9==0
def blocks_check(i,j): return i//27==j//27 and (i%9)//3==(j%9)//3
def show(grid):
print "".join(num+" "+" "*((i+1)%3==0)+"\n"*((i+1)%9==0) for i,num in enumerate(grid))
def solve(grid):
i=grid.find("0")
if i==-1:
show(grid)
return 0
temp=set()
for pos in range(81):
if row_check(i,pos) or col_check(i,pos) or blocks_check(i,pos):
temp.add(grid[pos])
for num in digits:
if num not in temp:
solve(grid[:i]+num+grid[i+1:])
return 0
grids ="026000810300708006400050007050107090003905100040302050100030002500204009038000460"
print "Given grid:"
show(grids)
print "Number of empty fields",grids.count("0")
print "Number of checks using bruteforce is > %18d"%grids.count("0")**9
digits="123456789"
print "Solution using backtracking :"
cProfile.run("solve(grids)")
"""
Outputs:
Given grid:
0 2 6 0 0 0 8 1 0
3 0 0 7 0 8 0 0 6
4 0 0 0 5 0 0 0 7
0 5 0 1 0 7 0 9 0
0 0 3 9 0 5 1 0 0
0 4 0 3 0 2 0 5 0
1 0 0 0 3 0 0 0 2
5 0 0 2 0 4 0 0 9
0 3 8 0 0 0 4 6 0
Number of empty fields 47
Number of checks using bruteforce is > 1119130473102767
Solution using backtracking :
7 2 6 4 9 3 8 1 5
3 1 5 7 2 8 9 4 6
4 8 9 6 5 1 2 3 7
8 5 2 1 4 7 6 9 3
6 7 3 9 8 5 1 2 4
9 4 1 3 6 2 7 5 8
1 9 4 8 3 6 5 7 2
5 6 7 2 1 4 3 8 9
2 3 8 5 7 9 4 6 1
34792 function calls (34648 primitive calls) in 0.029 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.029 0.029 <string>:1(<module>)
145/1 0.016 0.000 0.029 0.029 sud.py:11(solve)
11664 0.004 0.000 0.004 0.000 sud.py:3(row_check)
10368 0.004 0.000 0.004 0.000 sud.py:4(col_check)
9216 0.004 0.000 0.004 0.000 sud.py:5(blocks_check)
1 0.000 0.000 0.000 0.000 sud.py:7(show)
82 0.000 0.000 0.000 0.000 sud.py:8(<genexpr>)
3024 0.001 0.000 0.001 0.000 {method 'add' of 'set' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
145 0.000 0.000 0.000 0.000 {method 'find' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects}
144 0.000 0.000 0.000 0.000 {range}
"""
"""
/*
The C++ Version of the above code
*/
#include"bits/stdc++.h"
using namespace std;
int check_row(int i ,int j){
return i/9==j/9;
}
int check_col(int i, int j){
return (i-j)%9==0;
}
int check_block(int i , int j){
return i/27==j/27 && (i%9)/3==(j%9)/3 ;
}
int solve(char *x){
int i=0,cur=-1;
for (i=0;x[i]!='\0';i++){
if(x[i]=='0'){
cur=i;
break;
}
}
if(cur==-1){
int l=0;
for(l=0;l<81;l++){
printf("%c ",x[l]);
if((l+1)%3==0)printf(" ");
if((l+1)%9==0)puts("");
}
return 0;
}
set<char> myset;
myset.clear();
for(int k=0;k<81;k++){
if(check_row(cur,k)||check_col(cur,k)||check_block(cur,k)){
myset.insert(x[k]);
}
}
char d[10]="123456789";
int j=0;
for (j=0;j<9;j++){
if(d[j]!=(*myset.find(d[j]))){
char temp[82];
for(int n=0;n<81;n++){
temp[n]=(n==i?d[j]:x[n]);
}
solve(temp);
}
}
}
int main(){
char grids[82] ="026000810300708006400050007050107090003905100040302050100030002500204009038000460";
int o=0;
for(o=0;o<81;o++){
printf("%c ",grids[o]);
if((o+1)%3==0)printf(" ");
if((o+1)%9==0)puts("");
}
puts("");
cout<<"Solution: d"<<endl;
solve(grids);
}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment