LeetCode37.Sudoku solver电脑来解数独

原创 2016年05月31日 20:07:45


#include <vector>
#include <iostream>
#include "cocos2d.h"
using namespace std;
using namespace cocos2d;


void printvector(vector<vector<char>>& board)
{
log("----------------------------------");
for (int i = 0; i < board.size(); i++) {
string s;
for (int j = 0; j < board[i].size(); j++) {
s.append(" ");
s.append(string(1, board[i][j]));
}
log(s.c_str());
}
}


struct Vex{
int row;
int col;
int val;
Vex(int r, int c, int v) : row(r), col(c), val(v){}
};


class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
vector<Vex> vi;
int i = 0;
for (; i < board.size(); i++) {
int j = 0;
for (; j < board[i].size(); j++) {
if (board[i][j] == '.') {
int v = 1;
while (true) {
for (; v <= 9; v++) {
if (isOk(board, i, j, v + '0')) {
break;
}
}
if (v == 10) {
//printvector(board);
v = vi.back().val+1;
i = vi.back().row;
j = vi.back().col;
board[i][j] = '.';
vi.pop_back();
}
else {
vi.push_back(Vex(i, j, v));
board[i][j] = '0' + v;
break;
}
}
}
}
}
}
bool isOk(vector<vector<char>>& board, int row, int col, char val)
{
for (int c = 0; c < 9; c++) {
if (board[row][c] == val) {
return false;
}
if (board[c][col] == val) {
return false;
}
if (board[(row / 3)*3 + c / 3][(col / 3)*3 + c % 3] == val) {
return false;
}
}
return true;
}
};




void sudoku_test()
{

vector<string> sv{ "..9748...", "7........", ".2.1.9...", "..7...24.", ".64.1.59.", ".98...3..", "...8.3.2.", "........6", "...2759.." };
vector<vector<char>> vvc;
for (int i = 0; i < sv.size(); i++) {
vvc.push_back(vector<char>(sv[i].begin(), sv[i].end()));
}
Solution s;
printvector(vvc);
s.solveSudoku(vvc);
printvector(vvc);


}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Leetcode #37. Sudoku Solver 数独求解 结题报告

1 解题报告首先可以看下昨天的 Leetcode #36. Valid Sudoku 数独游戏验证 解题报告 数独的求解,这道题可以假设给定的这个是正确的做法就只能是暴力的回溯了。。 方法是回溯,用...
  • MebiuW
  • MebiuW
  • 2016年04月08日 23:58
  • 844

LeetCode 37 Sudoku Solver(求解数独)(*)

翻译写一个程序来通过填充空格求解数独。空格用'.'表示。你可以假定这里只有唯一解。(示例图片看下文)原文代码这道题我没写……不过为了博客的连续性,先凑一篇占个位置,以后再修改。class Soluti...
  • NoMasp
  • NoMasp
  • 2015年12月02日 12:47
  • 2078

LeetCode-37. Sudoku Solver (JAVA)(数独游戏解集)

LeetCode-37. Sudoku Solver (JAVA)(数独游戏解集)

[LeetCode 36&37] Valid Sudoku & Sudoku Solver (数独问题)

题目链接:valid-sudoku import java.util.Arrays; /** * Determine if a Sudoku is valid, according...
  • ever223
  • ever223
  • 2015年03月31日 11:09
  • 527

LeetCode-37.Sudoku Solver

https://leetcode.com/problems/sudoku-solver/ Write a program to solve a Sudoku puzzle by fillin...

LeetCode 37 - Sudoku Solver

Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ar...
  • ywcpig
  • ywcpig
  • 2016年03月01日 21:37
  • 274

(Java)LeetCode-37. Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by t...

Leetcode 37 Sudoku Solver 深搜基础题+位运算

强烈推荐的算法入门题,填数独,深搜方法完成,采用的标记方法依然和上一题一样,位操作,代码已详细给出注释。 Write a program to solve a Sudoku puzzle by fil...

DAY22:leetcode #37 Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by t...

37. Sudoku Solver Leetcode Python

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode37.Sudoku solver电脑来解数独
举报原因:
原因补充:

(最多只允许输入30个字)