Backtracking Algorithms-Sudoku Problem

The idea of backtracking algorithm: every intersection A, choose a road to go a, if a fails, then go back to the intersection A, choose one of the other bcd, go. If it still doesn't work, then go back to the intersection before A and repeat the above operation.

The classical problems solved by backtracking algorithm are Sudoku, Eight Queens, 0-1 knapsack, graph coloring, traveling salesman problem, Full Permutation and so on.

Sudoku problem

// This method has some problems. The result is not right. It will be revised later.
#include<iostream>
using namespace std;


static bool sign = false;
static int **sudoku = new int*[9];
void init()
{
	for(int i = 0; i < 9; ++i)
	{
		sudoku[i] = new int[9]();
	}
}

// Judging whether the number filled in the blank position meets the requirements in rows and columns
bool Judge1(int x, int y, int n)	// Row and column, place n
{
	int i;
	for(i = 0; i < 9; ++i)
	{
		// Determine whether this number exists in a column
		if(sudoku[i][y] == n && i != x)
		{
			return false;
		}
		// Judging rows, the number exists
		if(sudoku[x][i] == n && i != y)
		{
			return false;
		}
	}
	return true;
}

// Judging whether the number filled in the blank space meets the requirement in the Nine-palace space
bool Judge2(int x, int y, int n)
{
	int xx, yy, i, j;
	xx = x/3;	// Block 0 1 2 in the row
	yy = y/3;	// Block 0 1 2 of the column
	for(i == xx*3; i < xx*3+3; ++i)		// Cycle of three rows in a block
	{
		for(j = yy*3; j < yy*3+3; ++j)	// Circulation of three columns in a block
		{
			if(sudoku[i][j] == n)	// Find this number
			{
				if(i == x && j == y)	// What we found was n itself.
					continue;
				else
					return false;
			}
		}
	}
	return true;
}

// Fill in blank arrays
bool Fill(int m)
{
	int n, x, y;
	x = m/9;	// That's ok
	y = m%9;	// column
	if(m > 80)
	{	
		sign = true;	// Because the program scan two-dimensional is actually scan one-dimensional, so there will be 81. 81 out.
		return true;	// Feelings should be false, not true
	}
	if(sudoku[x][y] == 0)
	{
		for(n = 1; n <= 9; ++n)		// n is data to fill in for windfall profits
		{
			sudoku[x][y] = n;	// Filled digit
			if(Judge1(x, y, n)&&Judge2(x, y, n))	// Judge whether the number is reasonable
			{	
			//	sudoku[x][y] = n; // fill in numbers
				if(Fill(m))		// Continue to drop a position, recurse, and finally return to true in turn.
					return true;
			}
			sudoku[x][y] = 0;	// If not, reset to 0, and then continue to judge.
		}
	}
	else
		return Fill(m+1);	// If the previous position has a value, fill in the next position and scan line by line.
	return false;
}

void print(int **sudoku)
{
	int i, j;
	for(i = 0; i < 9; ++i)
	{
		for(j = 0; j < 9; ++j)
		{
			cout << sudoku[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void Delete()
{
	for(int i = 0; i < 9; ++i)
	{
		delete[]sudoku[i];
		sudoku[i] = NULL;
	}
}

int main()
{
	init();	// Open up space for solitary arrays
	int i, j, k;
	cout << "Input the original Sudoku data and replace the blank with 0" << endl;
	for(i = 0; i < 9; ++i)
	{
		for(j = 0; j < 9; ++j)
			cin >> sudoku[i][j];
	}
	//print(sudoku);
	//Beginning Sudoku Function
	cout << "////////**************///////////" << endl << endl;
	if(Fill(0))	// Fill in from the first position
	{
		for(i = 0; i < 9; ++i)
		{
			for(j = 0; j < 9; ++j)
			{
				cout << sudoku[i][j] << " ";
				if(!((j + 1)%3)) 
				{
					cout << "|";		// Every three numbers, make a line.
				}
			}
			cout << endl;
			if(!((i + 1)%3))
			{
				for(k = 0; k < 12; ++k)
                                        cout << "__";
                                cout << endl;
			}
		}
	}
	else
		cout << "The number of unique, please check whether the input is correct!" << endl;
	Delete();
	return 0;
}

/*
8 0 0 0 0 0 0 0 0
0 0 3 6 0 0 0 0 0
0 7 0 0 9 0 2 0 0 
0 5 0 0 0 7 0 0 0
0 0 0 0 4 5 7 0 0 
0 0 0 1 0 0 0 3 0
0 0 1 0 0 0 0 6 8
0 0 8 5 0 0 0 1 0
0 9 0 0 0 0 4 0 0
0 0 0 0 0 1 2 0 0 
0 6 0 0 5 0 0 0 7
0 0 0 3 0 0 0 0 4
0 0 0 4 0 0 0 3 0
0 0 0 0 0 2 1 0 0 
0 0 8 0 7 0 0 0 6
0 9 2 0 0 0 0 0 0
7 8 0 0 0 0 0 0 5
6 0 5 0 9 0 0 0 0
 
*/

 

Posted by sfhc on Fri, 04 Oct 2019 08:19:23 -0700