关闭

036 - Valid Sudoku

136人阅读 评论(0) 收藏 举报
分类:

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.


检查当前的数独是否合法,不要求有解,检查是否符合数独定义的规则


int locok(char **sudu, int x, int y, int num)
{
     int i, j, i1, j1;
     for (i = 0; i < 9; i++)
         if (i != x && sudu[i][y] == num)
                 return 0;
     for (i = 0; i < 9; i++)
         if (i != y && sudu[x][i] == num)
                 return 0;
     i = x / 3 * 3;
     j = y / 3 * 3;
     i1 = i + 3;
     j1 = j + 3;
     for (; 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;
}

bool isValidSudoku(char** board, int boardRowSize, int boardColSize) 
{
	int i, j, ret = 1;
	for (i = 0; i < boardRowSize; i++)
		for (j = 0; j < boardColSize; j++) {
			if (board[i][j] == '.') continue;
			ret &= locok(board, i, j, board[i][j]);
		}
	return ret;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28455次
    • 积分:1507
    • 等级:
    • 排名:千里之外
    • 原创:124篇
    • 转载:10篇
    • 译文:0篇
    • 评论:0条
    博客专栏