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. ural 1160

    最小生成树  第一次敲 套用几个函数 其实挺容易的 #include <cstdio> #include <cstring> #include <vector> # ...

  2. 认识RGB和YUV

    多年来,对于大部分人来说,对图形信号的认识不外有三种:射频信号,复合视频信号,S视频信号.射频信号是由复合视频信号调到高频上,普通电视机的天线输入信号用于射频信号,复合视频信号的输入出是用RGA端子. ...

  3. html5移动web开发实战必读书记

    原文  http://itindex.net/detail/50689-html5-移动-web 主题 HTML5 一.配置移动开发环境 1.各种仿真器.模拟器的下载安装 http://www.mob ...

  4. CSU1321+SPFA

    简单题 /* 简单的bfs */ #include<algorithm> #include<iostream> #include<string.h> #includ ...

  5. POJ2528+线段树

    见代码. /* 线段树+Lazy 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度. 现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW. 后贴的海 ...

  6. [itint5]环形最大连续子段和

    http://www.itint5.com/oj/#9 一开始有了个n*n的算法,就是把原来的数组*2,由环形的展开成数组.然后调用n次最大子段和的方法.超时. 后来看到个O(n)的算法,就是如果不跨 ...

  7. POJ3660——Cow Contest(Floyd+传递闭包)

    Cow Contest DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a prog ...

  8. windows下使用远程工具登录虚拟机上的Linux、访问虚拟机上的服务 、端口转发、win7 telnet登陆虚拟机

    首先要清楚virtual box如何设置端口转发: 一篇文章: 如何使用VirtualBox进行端口转发 由于默认的方式是用NAT来做虚拟机网络的,因此如果从外网想访问虚拟机的应用会比较麻烦.以前一直 ...

  9. 转:JS日期加减,日期运算

    原文 出处http://hi.baidu.com/tonlywang/item/685fba8933a2a756e73d1950 一.日期减去天数等于第二个日期 function cc(dd,dadd ...

  10. python 简单示例说明os.walk和os.path.walk的不同

    import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...