rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void refresh();
int power(int a, int b);
int checkBlocks(int b);
void printBlocks();
int createBlocks();
int col[9], mat[3][9], tcol[9], row[9], block[9], fin, ci, ri, bi, r, i, pr, ud, d;
void refresh(int complete)
{
for(fin=0;fin<9;fin++)
{
if (complete) col[fin]=0;
tcol[fin]=0;
row[fin]=0;
block[fin]=0;
}
ci=0;
ri=0;
bi=0;
fin=0;
}
int main(int argc, char *argv[])
{
if (argc!=2)
{
printf("[difficulty (1-9)]\n");
return 1;
}
ud = (*argv[1]-48);
if (ud<1)
{
printf("error: invalid, setting to 1\n");
ud = 1;
}
srand(time(NULL));
while(createBlocks(col));
while(createBlocks(col));
while(createBlocks(col));
return 0;
}
int power(int a, int b)
{
int ret = 1, i=0;
for(i=0;i<b;i++) ret = ret * a;
return ret;
}
int checkBlocks(int b)
{
int block[3];
block[0]=power(2,(mat[b*3][0])-1)+power(2,(mat[b*3][1])-1)+power(2,(mat[b*3][2])-1) + power(2,(mat[(b*3)+1][0])-1)+power(2,(mat[(b*3)+1][1])-1)+power(2,(mat[(b*3)+1][2])-1) + power(2,(mat[(b*3)+2][0])-1)+power(2,(mat[(b*3)+2][1])-1)+power(2,(mat[(b*3)+2][2])-1);
if ((block[0] & 511)!=511) return 1;
block[1]=power(2,(mat[b*3][3])-1)+power(2,(mat[b*3][4])-1)+power(2,(mat[b*3][5])-1) + power(2,(mat[(b*3)+1][3])-1)+power(2,(mat[(b*3)+1][4])-1)+power(2,(mat[(b*3)+1][5])-1) + power(2,(mat[(b*3)+2][3])-1)+power(2,(mat[(b*3)+2][4])-1)+power(2,(mat[(b*3)+2][5])-1);
if ((block[1] & 511)!=511) return 1;
block[2]=power(2,(mat[b*3][6])-1)+power(2,(mat[b*3][7])-1)+power(2,(mat[b*3][8])-1) + power(2,(mat[(b*3)+1][6])-1)+power(2,(mat[(b*3)+1][7])-1)+power(2,(mat[(b*3)+1][8])-1) + power(2,(mat[(b*3)+2][6])-1)+power(2,(mat[(b*3)+2][7])-1)+power(2,(mat[(b*3)+2][8])-1);
if ((block[2] & 511)!=511) return 1;
else return 0;
}
void printBlocks()
{
int aa, bb;
for(aa=0;aa<3;aa++)
{
for(bb=0;bb<9;bb++)
{
if ((rand() % 10) < ud) printf("* ");
else printf("%d ",mat[aa][bb]);
}
printf("\n");
}
}
int createBlocks(int col[9])
{
refresh(0);
while(fin++<3)
{
while(row[ri]<511)
{
r = (rand() % 9) + 1;
pr = power(2,r-1);
if (((pr & (tcol[ci] | col[ci]))==0) && (((pr & row[ri]))==0))
{
row[ri] = row[ri] + pr;
mat[ri][ci]=r;
tcol[ci] = tcol[ci] + pr;
ci++;
} else if ((row[ri] | (tcol[ci] | col[ci]))==511) return 1;
}
if (ri==2)
{
if (checkBlocks(0)) return 1;
else
{
for(i=0;i<9;i++) col[i]=(tcol[i] | col[i]);
printBlocks();
return 0;
}
}
ci=0;
ri++;
}
return 0;
}