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

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

leecode 解題總結:36. Valid Sudoku

2019-11-10 18:46:29
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>#include <string>#include <fstream>using namespace std;/*問題:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially filled sudoku which is valid.Note:A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.分析:實質就是根據現有數獨中已經填充的數字,計算是否有解。數獨中每一行和每一列必須包含1~9的數字3*3的數組中必須包含1~9每個數字。此題關鍵是從何處開始遍歷,判斷是否能夠組成數組:采用每橫或每豎列是否每個數字只出現一次,對于每個棋盤判斷每個數字是否只出現一次;一旦不符合,重新嘗試填新的數字。實際上就是遞歸來做。關鍵是填充新的數字必須滿足已有條件:1:填充的數字已有出現次數不能超過總數題目中說判斷一個數獨棋盤是否有效,不需要一定有解,只需要判定該棋盤是否滿足其規則采用暴力破解:遍歷每一行,每一列分別看每個元素是否出現次數<=1次,輸入:9(數獨的長度,寬度與之相等)53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79輸出:yes關鍵:1 判斷子棋盤是否有效,可以遍歷一遍棋盤,每次計算出子棋盤的編號k=i /3 * 3 + j / 3例如(0,5)在第2個棋盤中,對應下標為1for(int i = 0 ; i < size ; i++){	for(int j = 0 ; j < size ; j++ )	{		if('.' == board.at(i).at(j))		{			continue;		}		//計算子棋盤編號		subBoard = i / 3 * 3 + j /3 ;		num = board[i][j] - '0' - 1;//多減1是因為下標從0開始		if(usedRow[i][num] || usedCol[j][num] || usedSubBoard[subBoard][num])		{			return false;		}		usedRow[i][num] = usedCol[j][num] = usedSubBoard[subBoard][num] = 1;	}}*/class Solution{public:	bool isValidSudoku(vector<vector<char> > &board)	{		if(board.empty())		{			return false;		}		int size = board.size();		int usedRow[9][9] = {0};		int usedCol[9][9] = {0};		int usedSubBoard[9][9] = {0};		int subBoard;		int num;		for(int i = 0 ; i < size ; i++)		{			for(int j = 0 ; j < size ; j++ )			{				if('.' == board.at(i).at(j))				{					continue;				}				//計算子棋盤編號				subBoard = i / 3 * 3 + j /3 ;				num = board[i][j] - '0' - 1;//多減1是因為下標從0開始				if(usedRow[i][num] || usedCol[j][num] || usedSubBoard[subBoard][num])				{					return false;				}				usedRow[i][num] = usedCol[j][num] = usedSubBoard[subBoard][num] = 1;			}		}		return true;	}};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 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);	}	bool result = solution.isValidSudoku(board);	if(result)	{		cout << "yes" << endl;	}	else	{		cout << "no" << endl;	}}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五常市| 平度市| 河津市| 会理县| 桐乡市| 拉萨市| 图片| 宣化县| 栾城县| 甘洛县| 桃园市| 武夷山市| 施秉县| 银川市| 曲沃县| 崇义县| 新乡市| 弋阳县| 浦东新区| 石城县| 张家口市| 台东市| 永昌县| 西丰县| 隆子县| 托克逊县| 宝鸡市| 朔州市| 崇义县| 东山县| 锦屏县| 色达县| 措勤县| 青海省| 马鞍山市| 陈巴尔虎旗| 凌云县| 吉林省| 肇州县| 饶平县| 铜川市|