# 037 - Sudoku Solver

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.

static int find = 0;
int locok(char **sudu, int x, int y, int num)
{
int i, j, i1, j1;
if (sudu[x][y] != '.') return 2;
/* check row */
for (i = 0; i < 9; i++)
if (i != x && sudu[i][y] == num)
return 0;
/* check col */
for (i = 0; i < 9; i++)
if (i != y && sudu[x][i] == num)
return 0;
/* check area */
i = x / 3 * 3;
j = y / 3 * 3;
i1 = i + 3;
j1 = j + 3;
for (i = i1 - 3; i < i1; i++)
for (j = j1 - 3; j < j1; j++){
if (i == x && j ==y) continue;
if (sudu[i][j] == num)
return 0;
}

return 1;
}

int xsudu(char *sudu[], int x, int y)
{
if (x > 8 || y > 8 || x < 0 || y < 0) return -1;
int i;
if (x == 8 && y == 8) {
for (i = 1; i < 10; i++) {
int ok = locok(sudu, x, y, i + '0');
if (!ok) continue;
if (ok == 1)
sudu[x][y] = i + '0';
return find = 1;
}
} else {
for (i = 1; i < 10; i++) {
int ok = locok(sudu, x, y, i + '0');
if (!ok) continue;
if (ok == 1) sudu[x][y] = i + '0';
xsudu(sudu, y == 8? x + 1:x, y == 8? 0:y + 1);
if (find) return 0;
if (ok == 1) sudu[x][y] = '.';
if (ok == 2) break;
}
}

return 0;
}

void solveSudoku(char** board, int boardRowSize, int boardColSize)
{
find = 0;
xsudu(board, 0, 0);
}

• 本文已收录于以下专栏：

## LeetCode: Sudoku Solver

• AIvin24
• 2014年06月22日 18:52
• 1879

## LeetCode 37 Sudoku Solver (C,C++,Java,Python)

Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ar...
• runningtortoises
• 2015年05月19日 13:11
• 2037

## Leetcode 37 Sudoku Solver

• zxzxy1988
• 2013年02月17日 21:53
• 7777

## LeetCode 037 Sudoku Solver

• zhujf553
• 2016年05月21日 13:18
• 79

## leetcode 037 —— Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by...
• ziquanbang
• 2015年07月15日 21:42
• 76

## Sudoku Solver -- LeetCode

• linhuanmars
• 2014年03月10日 03:56
• 20330

## [leetcode-37]Sudoku Solver(java)

• zdavb
• 2015年09月01日 22:20
• 993

## LeetCode Sudoku Solver 数独C++程序

• kenden23
• 2013年12月05日 07:47
• 4033

## 37. Sudoku Solver leetcode python new season 2016

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by...
• hyperbolechi
• 2016年01月20日 11:50
• 289

## 37. Sudoku Solver Leetcode Python

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by...
• hyperbolechi
• 2015年03月27日 04:58
• 1759

举报原因： 您举报文章：037 - Sudoku Solver 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)