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/ 题目地 ...
随机推荐
- Log4J 如何分开Logger输出
今天和两个同事讨论Log4j,他们都需要解决一个问题,怎么分开输出Logger.这么讲不清楚,举个例子: package com.gmail.at.ankyhe.log4jtest; import o ...
- [Firefly引擎][学习笔记三][已完结]所需模块封装
原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读: 笔记三主要就是各个模块的封装了,这里贴 ...
- 荣誉,还是苦逼?| 也议全栈工程师和DevOps
引言 全栈工程师(本文称「全栈」开发者)和 DevOps 无疑是近期最火的词汇,无论是国外还是国内.而且火爆程度远超于想象. 全栈和 DevOps,究竟是我们的新职业方向,还是仅仅创业公司老板的心头所 ...
- c/c++ define用法
define,无参宏定义的一般形式为:#define 标识符 字符串 外文名 define 词条范围 计算机专业用语 无参一般形式 #define 标识符 字符串 带参一般形式 #define 宏名( ...
- C++11多线程教学(一)
本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...
- FF浏览器来帮助我们录制脚本
有时我们录制一个页面的脚本,我们需要知道这个页面哪些请求是耗时最大的?这个时候FF浏览器的网络分析功能就可以派上用场了,打开火狐浏览器按F12: 点击重新载入,可以看到下面的信息: 看到最耗时的操作了 ...
- 在Vim里使用gtags-cscope
用Vundle安装好gtags-cscope后,要在vimrc里添加如下设置: " cscopeset cscopetag " 使用 cscope ...
- (转)CAP理论十二年回顾:"规则"变了
编者按:由InfoQ主办的全球架构师峰会将于2012年8月10日-12日在深圳举行,为了更好地诠释架构的意义.方法和实践,InfoQ中文站近期会集中发布一批与架构相关的文章,本篇即为其中之一.Info ...
- JavaScript window.open()属性
一. Window 对象 Window 对象是 JavaScript 层级中的顶层对象. Window 对象代表一个浏览器窗口或一个框架. Window 对象会在 <body> 或 < ...
- LeetCode Pascal's Triangle II (杨辉三角)
题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...