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的更多相关文章

  1. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  2. [LeetCode] 36. Valid Sudoku 验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  3. leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

    leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...

  4. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  5. leetcode面试准备:Valid Anagram

    leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...

  6. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  7. 前端与算法 leetcode 36. 有效的数独

    目录 # 前端与算法 leetcode 36. 有效的数独 题目描述 概要 提示 解析 算法 传入[['5', '3', '.', '.', '7', '.', '.', '.', '.'],['6' ...

  8. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. uva 10780

    曾经做过一个类似的  求n!中有多少个质因子m 这里有一个结论 k = n/m+n/(m^2)+n/(m^3)+.... int getnum(int n, int m) { int sum = 0; ...

  2. 李洪强iOS开发本人集成环信的经验总结_08_自动登录补充

    李洪强iOS开发本人集成环信的经验总结_08_自动登录补充 来到Appdelegate里面 01 - 遵守自动登录的代理协议 02 - 设置自动登录的代理 03 - 判断与实现  04 - 代理方法的 ...

  3. 修改netbeans模版头部的说明

    以新建一个php类文件为例: 有两个地方需要修改, 1,工具->模版->默认许可证->在编辑器中打开 2,工具->模版->选择php类->在编辑器中打开 即可进行修 ...

  4. 你只是看起来很努力(只是做了一遍真题,草草的对了一遍答案,然后冲出自习室继续她学生会的事情了,骗自己更容易)good——想起了自己在六大时候的无奈

    (转)你只是看起来很努力一次上课,一个女孩子垂头丧气的跟我说,老师,我考了四次四级,还没过,究竟是为什么. 我说,你真题做了吗?单词背了吗?她拿出已经翻破了的真题,跟我说,你讲的所有的题目我连答案都记 ...

  5. SGU 101 AC

    总算AC了,好帅气的算法,同时解决了自环跟非连通,一种自下向上找环的算法过程,这里把欧拉回路讲得很清楚,赞. #include <iostream> #include <vector ...

  6. Sqlmap基础(二)

    sqlmap.py -r req1.txt --dbms Oracle --risk

  7. Android 进程保活招式大全

    目前市面上的应用,貌似除了微信和手Q都会比较担心被用户或者系统(厂商)杀死问题.本文对 Android 进程拉活进行一个总结. Android 进程拉活包括两个层面: A. 提供进程优先级,降低进程被 ...

  8. Java API ——String类

    1.String类概述 · 字符串是由多个字符组成的一串数据(字符序列),也可以看成是一个字符数组. · 字符串字符值“abc”也可以看成是一个字符串对象. · 字符串是常量,一旦被赋值,就不能被改变 ...

  9. python中os模块path.abspath()返回的并不是绝对值,而是个错误的不存在的拼接地址

    附截图: 当前路径:  a=r'D:\PCsync\python\commands'  为绝对路径 遍历出来的4条应该是D:\PCsync\python\commands\commands.py... ...

  10. 需要保存数据zabbix,不需要保存数据nagios

    需要保存数据zabbix,不需要保存数据nagios cacti 有什么好用的基于Web的Linux系统监控开源工具(网管系统) 要求类似于Ubuntu的Landscape,可以记录下历史CPU数值. ...