You are on page 1of 3

#include <iostream>

#include <omp.h>

int table[16][16] = {
{1, 0, 0, 2, 3, 4, 0, 0, 12, 0, 6, 0, 0, 0, 7, 0},
{0, 0, 8, 0, 0, 0, 7, 0, 0, 2, 0, 0, 9, 10, 6, 11},
{0, 12, 0, 0, 10, 0, 0, 1, 0, 13, 0, 11, 0, 0, 14, 0},
{3, 0, 0, 15, 2, 0, 0, 14, 0, 0, 0, 9, 0, 0, 12, 0},

{13, 0, 0, 0, 8, 0, 0, 16, 0, 12, 2, 0, 1, 15, 0, 0},


{0, 11, 7, 6, 0, 0, 0, 16, 0, 0, 0, 15, 0, 0, 5, 13},
{0, 0, 0, 10, 0, 5, 15, 0, 0, 4, 0, 8, 0, 0, 11, 0},
{16, 0, 0, 5, 9, 12, 0, 0, 1, 0, 0, 0, 0, 0, 8, 0},

{0, 2, 0, 0, 0, 0, 0, 13, 0, 0, 12, 5, 8, 0, 0, 3},


{0, 13, 0, 0, 15, 0, 2, 0, 0, 14, 8, 0, 16, 0, 0, 0},
{5, 8, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 13, 9, 15, 0},
{0, 0, 12, 4, 0, 6, 16, 0, 13, 0, 0, 7, 0, 0, 0, 5},

{0, 3, 0, 0, 12, 0, 0, 0, 6, 0, 0, 4, 11, 0, 0, 6},


{0, 7, 0, 0, 16, 0, 5, 0, 14, 0, 0, 1, 0, 0, 2, 0},
{11, 11, 15, 9, 0, 0, 13, 0, 0, 2, 0, 0, 0, 14, 0, 0},
{0, 14, 0, 0, 0, 0, 0, 2, 0, 0, 13, 3, 5, 0, 0, 12}
};

void showTable(int[16][16]);
bool mixer(int[16][16]);
int incomplete(int[16][16], int*, int*);
bool validity_check(int[16][16], int, int, int);

using namespace std;


int main()
{
if (mixer(table))
{
cout << " RESUELTO! " << endl;
cout << "-------------------------" << endl;
showTable(table);
}
else
{
cout << "Solucion no encontrada !";
}
return 0;
}

bool mixer(int table[16][16] ) {


int row = 0;
int col = 0;
int x = -1;
int y = -1;

if (!incomplete(table, &row, &col)) {


return 1;
}

for (int i = 1; i <= 16; i++)


{
if (validity_check(table, i, row, col))
{
cout <<" valor: "<< i <<" row: " <<row << " col: "<<col << endl;
table[row][col] = i;
if (mixer(table))
{
return true;
}
table[row][col] = 0;
}
}
return false;
}

bool validity_check(int sudoku[16][16], int n, int p, int q)


{
for (int i = 0; i < 16; i++)
{
if (sudoku[p][i] == n && q != i)
{
return false;
}
}
for (int i = 0; i < 16; i++)
{
if (sudoku[i][q] == n && p != i)
{
return false;
}
}
int bx = q / 4;
int by = p / 4;
for (int i = by * 4; i < by * 4 + 4; i++)
{
for (int j = bx * 4; j < bx * 4 + 4; j++)
{
if (sudoku[i][j] == n && i != p && j != q)
{
return false;
}
}
}
return true;
}

int incomplete(int table[16][16], int* rowPoint, int* columnPoint)


{
for (*rowPoint = 0; *rowPoint < 16; (*rowPoint)++)
{
for (*columnPoint = 0; *columnPoint < 16; (*columnPoint)++)
{
if (table[*rowPoint][*columnPoint] == 0)
{
return true;
}
}
}
return false;
}

/*
Only print table
*/
void showTable(int sudoku[16][16])
{
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
cout << sudoku[i][j] << " ";
}
cout << endl << endl;
}
}

You might also like