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

首頁 > 學院 > 開發設計 > 正文

leecode 解題總結:37 Sudoku Solver

2019-11-10 17:25:46
字體:
來源:轉載
供稿:網友
#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.分析:這是搜索是否有解的問題,廣度優先搜索最優解,深度優先搜索確定是否有解。因此本題應該用深度優先搜索??搭}目,如果擺放新的元素不成功,應該是要回溯的。因此,此題應該是回溯來做。每一次嘗試擺放一個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等份,然后取余數	比如給定元素(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等份,然后取余數			比如給定元素(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))				{					//嘗試在空白的區域處擺放下一個元素,這里直接用cha					for(char c = '1' ; c <= '9' ; c++)					{						//如果擺放有效,繼續處理						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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通榆县| 通辽市| 溧阳市| 类乌齐县| 额济纳旗| 公安县| 清流县| 噶尔县| 新营市| 鹤岗市| 景洪市| 桑日县| 宜川县| 黄龙县| 绍兴市| 宿迁市| 临漳县| 高陵县| 卫辉市| 阳高县| 新和县| 莱西市| 酒泉市| 全椒县| 呼玛县| 鄂伦春自治旗| 顺平县| 时尚| 肇州县| 望奎县| 平谷区| 自贡市| 宁明县| 隆回县| 婺源县| 布尔津县| 页游| 阿克| 巴林右旗| 天门市| 永仁县|