Skip to content

Instantly share code, notes, and snippets.

@yuikns
Last active Aug 29, 2015
Embed
What would you like to do?
#include <string.h>
int hm[9][9];
int vm[9][9];
int am[9][9];
const int sz_m = sizeof(hm);
void init(char** a)
{
memset(hm,0,sz_m);
memset(vm,0,sz_m);
memset(am,0,sz_m);
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
int v = a[i][j] - '1';
if(v >= 0 && v < 9) {
hm[i][v] = vm[j][v] = am[i/3*3+j/3][v]=1;
}
}
}
}
int next(char** a,int offset)
{
if(offset >= 81) return 1;
char i;
int abs = offset/9;
int ord = offset%9;
if(a[abs][ord] != '.') return next(a,offset+1);
int area = abs/3*3 + ord/3;
for(i=0;i<=9;i++)
{
if(hm[abs][i]||vm[ord][i]||am[area][i]) continue;
a[abs][ord] = i + '1';
hm[abs][i] = vm[ord][i] = am[area][i] = 1;
if(next(a,offset+1)) return 1;
hm[abs][i] = vm[ord][i] = am[area][i] = 0;
a[abs][ord] = '.';
}
return 0;
}
void solveSudoku(char** board, int boardRowSize, int boardColSize) {
init(board);
next(board,0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment