国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

leecode 解題總結:37 Sudoku Solver

2019-11-10 18:09:21
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>#include <fstream>using namespace std;/*問題:Write a PRogram to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku puzzle......and its solution numbers marked in red.分析:這是搜索是否有解的問題,廣度優(yōu)先搜索最優(yōu)解,深度優(yōu)先搜索確定是否有解。因此本題應該用深度優(yōu)先搜索??搭}目,如果擺放新的元素不成功,應該是要回溯的。因此,此題應該是回溯來做。每一次嘗試擺放一個1~9中的某個元素,如果擺放不成功,就用另一個元素替換,如果最終棋盤擺滿了,就輸出結果。如何判定沒有結果,如果1判斷是否滿足可行解,只需要當前board[i][j]元素為c是否在行號,列號,子棋盤擺放過	判斷子棋盤上的9個元素是否與給定元素重復,先確定 子棋盤的行號=行號	給定位置(row,jcol)計算對應子棋盤的行號(x,y)	x = 3 * (row / 3)	y = 3 * (col / 3)	子棋盤編號 = 3 *(row / 3) + col /3 = 3 + 2 = 5,行號確定大的子棋盤編號,列號確定小的子棋盤編號	子棋盤元素下標 = 3 * (col / 3) + col % 3 = 3 * 2 + 8 % 3 = 8,其實就是將列分成3等份,然后取余數(shù)	比如給定元素(5,8),對應第6子棋盤中(編號為5)中第8個元素	這里由于采用i為0~8,i默認為列號	則新的子棋盤編號=board[ 3 * (row / 3) + i / 3 ][3 * (col / 3) + i % 3]	subBoardIndex = 3 * (row / 3) + i / 3;	index = 3 * (col / 3) + i % 3;*/class Solution {public:	bool isValidSudoku(vector<vector<char> > &board , int row  , int col , char c)	{		if(board.empty())		{			return false;		}		int size = board.size();		int subBoardIndex;		int index;		for(int i = 0 ; i < 9 ; i++)		{			if(board[i][col] != '.' && board[i][col] == c)			{				return false;			}			if(board[row][i] != '.' && board[row][i] == c)			{				return false;			}			/*			判斷子棋盤上的9個元素是否與給定元素重復,先確定 子棋盤的行號=行號			給定位置(row,jcol)計算對應子棋盤的行號(x,y)			x = 3 * (row / 3)			y = 3 * (col / 3)			子棋盤編號 = 3 *(row / 3) + col /3 = 3 + 2 = 5,行號確定大的子棋盤編號,列號確定小的子棋盤編號			子棋盤元素下標 = 3 * (col / 3) + col % 3 = 3 * 2 + 8 % 3 = 8,其實就是將列分成3等份,然后取余數(shù)			比如給定元素(5,8),對應第6子棋盤中(編號為5)中第8個元素			這里由于采用i為0~9,i默認為列號			則新的子棋盤編號=board[ 3 * (row / 3) + i / 3 ][3 * (col / 3) + i % 3]			*/			subBoardIndex = 3 * (row / 3) + i / 3;			index = 3 * (col / 3) + i % 3;			if(board[subBoardIndex][index] != '.' && board[subBoardIndex][index] == c)			{				return false;			}		}		return true;	}	bool isSolved(vector<vector<char>>& board)	{		if(board.empty())		{			return false;		}		int size = board.size();		for(int i = 0 ; i < size ; i++)		{			for(int j = 0 ; j < size ; j++ )			{				if('.' == board.at(i).at(j))				{					//嘗試在空白的區(qū)域處擺放下一個元素,這里直接用cha					for(char c = '1' ; c <= '9' ; c++)					{						//如果擺放有效,繼續(xù)處理						if(isValidSudoku(board , i , j , c))						{							board.at(i).at(j) = c;							//牛逼,直接用遞歸判斷下一次是否擺放成功							if(isSolved(board))							{								return true;							}							else							{								board.at(i).at(j) = '.';							}						}					}					//如果一直沒有得到結果,說明無效					return false;				}			}		}		return true;	}    void solveSudoku(vector<vector<char>>& board) {		if(board.empty())		{			return;		}		bool isSolve = isSolved(board);		_isSolved = isSolve;	}public:	bool _isSolved;};vector<string> readFile(string& fileName){	vector<string> results;	if(fileName.empty())	{		return results;	}	ifstream file(fileName , ios::in);	if(!file)	{		cout << "can't open file" << endl;		return results;	}	const int maxSize = 1024;	char str[maxSize];	while(!file.eof())	{		file.getline(str , maxSize);		string s(str);		results.push_back(s);	}	file.close();	return results;}void print(vector< vector<char> >& board){	if(board.empty())	{		cout << "no result" << endl;	}	int size = board.size();	for(int i = 0 ; i < size ; i++)	{		for(int j = 0 ; j < size ; j++ )		{			cout << board.at(i).at(j);		}		cout << endl;	}}void process(){	vector< vector<char> > board;	string s;	int size;	Solution solution;	board.clear();	vector<string> strs = readFile(string("data.txt"));	int len = strs.size();	for(int i = 0 ; i < len ; i++)	{		s = strs.at(i);		vector<char> str;		size = s.length();		for(int i = 0 ; i < size ; i++)		{			str.push_back(s.at(i));		}		board.push_back(str);	}	solution.solveSudoku(board);	if(solution._isSolved)	{		print(board);	}	else	{		cout << "no" << endl;	}}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 开化县| 屯昌县| 巴楚县| 绩溪县| 陇南市| 林芝县| 霍城县| 林口县| 两当县| 翁源县| 班戈县| 天镇县| 错那县| 深圳市| 新干县| 石渠县| 瑞丽市| 罗定市| 衡阳市| 松溪县| 陆丰市| 招远市| 宝山区| 襄汾县| 枣庄市| 泗洪县| 贵德县| 肇源县| 深水埗区| 梅河口市| 荔波县| 平阴县| 诸暨市| 青海省| 开封市| 高邑县| 遂宁市| 五河县| 佳木斯市| 汉阴县| 温泉县|