leetcode@ [36/37] Valid Sudoku / Sudoku Solver
https://leetcode.com/problems/valid-sudoku/
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.
class Solution {
public:
vector<int> getIdx(int i, int j) {
vector<int> idx();
int row, col;
if(i>= && i<=) {
idx[] = ; idx[] = ;
}
else if(i>= && i<=) {
idx[] = ; idx[] = ;
}
else if(i>= && i<=) {
idx[] = ; idx[] = ;
} if(j>= && j<=) {
idx[] = ; idx[] = ;
}
else if(j>= && j<=) {
idx[] = ; idx[] = ;
}
else if(j>= && j<=) {
idx[] = ; idx[] = ;
} return idx;
}
bool checkRowAndColumn(vector<vector<char>>& board, int i, int j) {
if(i< || i>=board.size() || j< || j>=board[].size()) return false; for(int ni=;ni<board.size();++ni) {
if(ni == i || board[ni][j] == '.') continue;
if(board[ni][j] == board[i][j]) return false;
} for(int nj=;nj<board[].size();++nj) {
if(nj == j || board[i][nj] == '.') continue;
if(board[i][nj] == board[i][j]) return false;
} return true;
} bool checkLocal(vector<vector<char>>& board, int i, int j) {
if(i< || i>=board.size() || j< || j>=board[].size()) return false; vector<int> idx = getIdx(i, j);
int li = idx[], ri = idx[], lj = idx[], rj = idx[]; for(int p=li;p<=ri;++p) {
for(int q=lj;q<=rj;++q) {
if((i==p && j==q) || board[p][q] == '.') continue;
if(board[i][j] == board[p][q]) return false;
}
} return true;
}
bool isValidSudoku(vector<vector<char>>& board) {
for(int i=;i<board.size();++i) {
for(int j=;j<board[i].size();++j) {
if(board[i][j] == '.') continue;
if(!checkLocal(board, i, j) || !checkRowAndColumn(board, i, j)) return false;
}
}
return true;
}
};
填写九宫格:
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.
class Solution {
public:
vector<int> getNext(vector<vector<char>>& board, int x, int y) {
vector<int> next; next.clear();
for(int j=y;j<board[].size();++j) {
if(board[x][j] == '.') {
next.push_back(x); next.push_back(j);
return next;
}
}
for(int i=x+;i<board.size();++i) {
for(int j=;j<board[i].size();++j) {
if(board[i][j] == '.') {
next.push_back(i); next.push_back(j);
return next;
}
}
}
return next;
}
vector<int> getIdx(int i, int j) {
vector<int> idx();
int row, col;
if(i>= && i<=) {idx[] = ; idx[] = ; }
else if(i>= && i<=) {idx[] = ; idx[] = ; }
else if(i>= && i<=) {idx[] = ; idx[] = ; } if(j>= && j<=) {idx[] = ; idx[] = ; }
else if(j>= && j<=) {idx[] = ; idx[] = ; }
else if(j>= && j<=) {idx[] = ; idx[] = ; } return idx;
}
bool checkRowAndColumn(vector<vector<char>>& board, int i, int j) {
if(i< || i>=board.size() || j< || j>=board[].size()) return false; for(int ni=;ni<board.size();++ni) {
if(ni == i || board[ni][j] == '.') continue;
if(board[ni][j] == board[i][j]) return false;
} for(int nj=;nj<board[].size();++nj) {
if(nj == j || board[i][nj] == '.') continue;
if(board[i][nj] == board[i][j]) return false;
} return true;
} bool checkLocal(vector<vector<char>>& board, int i, int j) {
if(i< || i>=board.size() || j< || j>=board[].size()) return false; vector<int> idx = getIdx(i, j);
int li = idx[], ri = idx[], lj = idx[], rj = idx[]; for(int p=li;p<=ri;++p) {
for(int q=lj;q<=rj;++q) {
if((i==p && j==q) || board[p][q] == '.') continue;
if(board[i][j] == board[p][q]) return false;
}
} return true;
} bool dfs(vector<vector<char>>& board, vector<vector<char>>& res, int x, int y) {
vector<int> next = getNext(board, x, y);
if(next.empty()) {
return true;
} int nx = next[], ny = next[];
for(int nn = ; nn <= ; ++nn) {
board[nx][ny] = nn + '';
if(checkLocal(board, nx, ny) && checkRowAndColumn(board, nx, ny)) {
if(dfs(board, res, nx, ny)) {
res[nx][ny] = nn + '';
return true;
}
}
}
return false;
}
void solveSudoku(vector<vector<char>>& board) {
vector<vector<char> > res = board;
dfs(board, res, , ); board = res;
}
};
leetcode@ [36/37] Valid Sudoku / Sudoku Solver的更多相关文章
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独
leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 前端与算法 leetcode 36. 有效的数独
目录 # 前端与算法 leetcode 36. 有效的数独 题目描述 概要 提示 解析 算法 传入[['5', '3', '.', '.', '7', '.', '.', '.', '.'],['6' ...
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- LA 4731
dp[i][j]意思是前i个分成j组最小的花费 #include<cstdio> #include<algorithm> #include<cstring> #in ...
- SSH架构简单总结
Struts.spring.Hibernate在各层的作用 1)struts 负责 web层. ActionFormBean 接收网页中表单提交的数据,然后通过Action 进行处理,再Forw ...
- vimrc for mac
" Configuration file for vim set modelines=0 " CVE-2007-2438 " Normally we use vim-ex ...
- Java IO3:字符流
字符流 字节流提供了处理任何类型输入/输出操作的功能(对于计算机而言,一切都是0 和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,一个Unicode字符占用2个字节,而 ...
- Android安全问题 抢先开机启动
导读:我们以如何抢先开机启动为例,来说明接收无序广播的静态广播接收器的接收顺序 (注意,文本只是陈述结果,所以叫结果篇,之后的文章再给出源码分析) 首先先说一下android中的广播和广播接收器 广播 ...
- log file sync
Recently, our application system has updated one app. I receive a email of complain the db server ch ...
- 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建
使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 目录 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 使用jenkins自动 ...
- linux编译注解
Linux kernel release 3.x <http://kernel.org/> These are the release notes for Linux version 3. ...
- JavaScript DOM高级程序设计 4.3控制事件流和注册事件侦听器--我要坚持到底!
一.事件流 我们通过下面一个实例,进行说明. <body> <h1>Event Flow</h1> <ul id="nav"> &l ...
- C#中的Marshal
Const.MaxLengthOfBufferd的长度固定为0x2000 也就是8192 private bool SendMessage(int messageType, string ip, ...