char* title = "Program to solve Challenger game.";
#include <stdio.h>
#include <string.h>
#define EOL "\r\n"
#define START 1
#define STOP 9
#define writeln(p) printf("%s" EOL, p)
#define zerofill(p) memset(p, 0, sizeof(p))
typedef unsigned char boolean;
int row_totals [4];
int column_totals [4];
int up_diagonal_total;
int down_diagonal_total;
struct
{
int v;
boolean known;
} a[4][4];
boolean valid(void)
{
int x;
int y;
int total;
for (x=0; x<4; x++)
{
total = 0;
for (y=0; y<4; y++)
total += a[x][y].v;
if (total != column_totals[x])
return 0;
}
for (y=0; y<4; y++)
{
total = 0;
for (x=0; x<4; x++)
total += a[x][y].v;
if (total != row_totals[y])
return 0;
}
total = 0;
for (x=0; x<4; x++)
total += a[x][x].v;
if (total != down_diagonal_total)
return 0;
total = 0;
for (x=0; x<4; x++)
total += a[x][3-x].v;
if (total != up_diagonal_total)
return 0;
return 1;
}
boolean increment(void)
{
int x;
int y;
for (x=0; x<4; x++)
{
for (y=0; y<4; y++)
{
if (!a[x][y].known)
{
a[x][y].v++;
if (a[x][y].v > STOP)
a[x][y].v = START;
else
return 0;
}
}
}
return 1;
}
void init(void)
{
zerofill(a );
zerofill(row_totals );
zerofill(column_totals );
up_diagonal_total = 0;
down_diagonal_total = 0;
}
void show_result(void)
{
int x;
int y;
printf("%d" EOL
, up_diagonal_total
);
for (y=0; y<4; y++)
{
for (x=0; x<4; x++)
printf("%d" EOL
, row_totals
[y
]);
}
for (x=0; x<4; x++)
printf("%d", column_totals
[x
]);
printf("%d" EOL
, down_diagonal_total
);
}
void main(void)
{
int x;
int y;
int count = 1;
int counter = 10000000;
writeln(title);
init();
// Set known values
// 32
// 9 31
// 6 28
// 8 28
// 7 33
// 30 33 31 26 28
a[1][0].v = 9; a[1][0].known = 1;
a[2][1].v = 6; a[2][1].known = 1;
a[0][2].v = 8; a[0][2].known = 1;
a[3][3].v = 7; a[3][3].known = 1;
row_totals [0] = 31;
row_totals [1] = 28;
row_totals [2] = 28;
row_totals [3] = 33;
column_totals [0] = 30;
column_totals [1] = 33;
column_totals [2] = 31;
column_totals [3] = 26;
up_diagonal_total = 32;
down_diagonal_total = 28;
for (x=0; x<4; x++)
{
for (y=0; y<4; y++)
if (!a[x][y].known)
a[x][y].v = START;
}
while (1)
{
counter--;
if (counter==0)
{
printf("count = %d" EOL
, count
++);
counter = 10000000;
}
if (valid())
{
show_result();
return;
}
if (increment())
break;
}
writeln("No solution");
}