Okej, nu är jag färdig. det blev lite klipp och klistra kod. Egentligen kunde jag nog löst det med function pointers men det blir för jobbigt för en så pass enkel uppgift.
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define ROW(x,y) (x*9+y)
#define COL(x,y) (y*9+x)
#define BOX(x,y) (boxList[x][y])
int boxList[9][9] = {
0, 1, 2,
9, 10, 11,
18, 19, 20,
3, 4, 5,
12, 13, 14,
21, 22, 23,
6, 7, 8,
15, 16, 17,
24, 25, 26,
27, 28, 29,
36, 37, 38,
45, 46, 47,
30, 31, 32,
39, 40, 41,
48, 49, 50,
33, 34, 35,
42, 43, 44,
51, 52, 53,
54, 55, 56,
63, 64, 65,
72, 73, 74,
57, 58, 59,
66, 67, 68,
75, 76, 77,
60, 61, 62,
69, 70, 71,
78, 79, 80
};
#define N1 0x8000
#define N2 0x4000
#define N3 0x2000
#define N4 0x1000
#define N5 0x0800
#define N6 0x0400
#define N7 0x0200
#define N8 0x0100
#define N9 0x0080
#define EM 0xFF80
int num[9] = {N1, N2, N3, N4, N5, N6, N7, N8, N9};
void PrintMap(int* map);
int Solve(int* map);
int main(int argc, char *argv[])
{
int map[81] = {
EM, N1, EM, N3, N4, EM, N9, EM, EM,
EM, EM, N2, EM, EM, N7, EM, N8, EM,
EM, EM, EM, EM, EM, EM, EM, EM, N6,
EM, EM, EM, EM, N9, N2, EM, N4, EM,
N3, EM, N8, EM, EM, EM, N7, EM, EM,
EM, EM, N7, N5, EM, EM, EM, N6, N1,
EM, N4, N6, N8, EM, EM, EM, EM, N2,
EM, N7, EM, EM, N1, EM, EM, N3, EM,
EM, N5, EM, N2, EM, N6, EM, EM, EM
};
if(Solve(map))
printf("success!!!!");
PrintMap(map);
system("PAUSE");
return 0;
}
void PrintMap(int* map) {
int i, j;
for(i=0;i<9;i++) {
for(j=0;j<9;j++)
switch(map[i*9+j]) {
case N1:
printf("1");
break;
case N2:
printf("2");
break;
case N3:
printf("3");
break;
case N4:
printf("4");
break;
case N5:
printf("5");
break;
case N6:
printf("6");
break;
case N7:
printf("7");
break;
case N8:
printf("8");
break;
case N9:
printf("9");
break;
default:
printf("*");
}
printf("\n");
}
}
int Solve(int* map) {
int n, i, j;
while(TRUE) {
int nothingChanged = TRUE;
// Check for sure numbers and make everything in the same
// row, col and box impossible.
for(n=0;n<81;n++) {
int row;
int col;
int box;
//check what row it belongs to
for(i=0;i<9;i++)
for(j=0;j<9;j++)
if(n == ROW(i, j))
row = i;
//check what col it belongs to
for(i=0;i<9;i++)
for(j=0;j<9;j++)
if(n == COL(i, j))
col = i;
//check what box it belongs to
for(i=0;i<9;i++)
for(j=0;j<9;j++)
if(n == BOX(i,j))
box = i;
// make all of the same row impossible
for(i=0;i<9;i++) {
if(ROW(row, i) == n)
continue;
for(j=0;j<9;j++)
if(map[ROW(row, i)] == num[j]) {
if((map[n] & (~map[ROW(row, i)])) != map[n])
nothingChanged = FALSE;
map[n] &= ~map[ROW(row, i)];
}
}
// make all of the same col impossible
for(i=0;i<9;i++) {
if(COL(col, i) == n)
continue;
for(j=0;j<9;j++)
if(map[COL(col, i)] == num[j]) {
if((map[n] & (~map[COL(col, i)])) != map[n])
nothingChanged = FALSE;
map[n] &= ~map[COL(col, i)];
}
}
// make all of the same box impossible
for(i=0;i<9;i++) {
if(BOX(box, i) == n)
continue;
for(j=0;j<9;j++)
if(map[BOX(box, i)] == num[j]) {
if((map[n] & (~map[BOX(box, i)])) != map[n])
nothingChanged = FALSE;
map[n] &= ~map[BOX(box, i)];
}
}
}
// Check every row, box and col, if the field only have one
// possible place for a number left, make all the other
// numbers impossible on that one.
// check rows
for(n=0;n<9; n++)
for(i=0; i<9; i++) {
int foundNum = FALSE;
int possibles = 0;
int lastPossible;
for(j=0; j<9; j++) {
if(map[ROW(n, j)] == num[i])
foundNum = TRUE;
if((map[ROW(n, j)] & num[i]) == num[i]) {
lastPossible = ROW(n, j);
possibles++;
}
}
if((possibles == 1) && (map[lastPossible] != num[i])) {
map[lastPossible] = num[i];
nothingChanged = FALSE;
}
}
// check cols
for(n=0;n<9; n++)
for(i=0; i<9; i++) {
int foundNum = FALSE;
int possibles = 0;
int lastPossible;
for(j=0; j<9; j++) {
if(map[COL(n, j)] == num[i])
foundNum = TRUE;
if((map[COL(n, j)] & num[i]) == num[i]) {
lastPossible = COL(n, j);
possibles++;
}
}
if((possibles == 1) && (map[lastPossible] != num[i])) {
map[lastPossible] = num[i];
nothingChanged = FALSE;
}
}
// check box:es
for(n=0;n<9; n++)
for(i=0; i<9; i++) {
int foundNum = FALSE;
int possibles = 0;
int lastPossible;
for(j=0; j<9; j++) {
if(map[BOX(n, j)] == num[i])
foundNum = TRUE;
if((map[BOX(n, j)] & num[i]) == num[i]) {
lastPossible = BOX(n, j);
possibles++;
}
}
if((possibles == 1) && (map[lastPossible] != num[i])) {
map[lastPossible] = num[i];
nothingChanged = FALSE;
}
}
if(nothingChanged)
break;
}
for(n=0;n<81;n++)
for(i=0;i<9;i++) {
if(map[n] == num[i])
break;
return FALSE;
}
return TRUE;
}