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. DJANGO的ORM的Q查询作多字段外键的模糊查询样码

    工作中用到的,存照一下. from django.db.models import Q if self.kwargs.has_key('search_pk'): search_pk = self.kw ...

  2. 关于PHP写APP接口的安全问题探讨(一)

    在探讨这个问题之前,先要确认一点的是,作为一名互联网Coder,无论你是前端或者后端你都要对http请求要有一定的了解,知道http特性,要清楚的了解http里面的Request与Response是什 ...

  3. nginx 详解

    #运行用户 #user  nobody;   #启动进程,通常设置成和cpu的数量相等或者2倍于cpu的个数(具体结合cpu和内存).默认为1 worker_processes  1;   #全局的错 ...

  4. 算法总结之欧拉函数&中国剩余定理

    算法总结之欧拉函数&中国剩余定理 1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)( ...

  5. C# 模拟POST提交文件

    http://blog.csdn.net/hellowjwang/article/details/19975635 public class HttpPost { /// <summary> ...

  6. P66、面试题8:旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...

  7. php中的ceil和floo以及round函数

    ceil是向上进位得到一个值的函数: floor是舍掉小数位得到一个值的函数: round是用来四舍五入的函数. ceil 定义和用法: ceil() 函数向上舍入为最接近的整数. ceil(x); ...

  8. PHP操作FTP类 (上传下载移动创建等)

    使用PHP操作FTP-用法 Php代码 收藏代码 <? // 联接FTP服务器 $conn = ftp_connect(ftp.server.com); // 使用username和passwo ...

  9. git设置忽略某些文件或文件夹

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.如果没有 .gitignore 文件,就自己创建一个,手动创建会提示你输入文件名称,因此,你 ...

  10. 1521. War Games 2(线段树解约瑟夫)

    1521 根据区间和 来确定第k个数在哪 #include <iostream> #include<cstdio> #include<cstring> #inclu ...