Chinaunix首页 | 论坛 | 博客

AAA

  • 博客访问: 19289
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 08:23
个人简介

好记性不如烂笔头,记录学习历程和随想云云,欢迎各位大虾拍砖

文章分类

全部博文(33)

文章存档

2015年(33)

我的朋友
最近访客

分类: C/C++

2015-05-05 23:05:46

【问题描述】

Determine if a Sudoku is valid, according to: .

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.

【解决方案】

点击(此处)折叠或打开

  1. bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
  2.     bool flag[9] = { 0 };
  3.     int i, j;

  4.     /* rows */
  5.     for (i = 0; i < boardRowSize; i++)
  6.     {
  7.         for (j = 0; j < boardColSize; j++)
  8.         {
  9.             if (board[i][j] != '.')
  10.             {
  11.                 int n = board[i][j] - '1'; /* digits start with 1 */
  12.                 if (flag[n]) return false;
  13.                 else flag[n] = true;
  14.             }
  15.         }
  16.         memset(flag, 0, sizeof(flag));
  17.     }

  18.     /* columns */
  19.     for (j = 0; j < boardColSize; j++)
  20.     {
  21.         for (i = 0; i < boardRowSize; i++)
  22.         {
  23.             if (board[i][j] != '.')
  24.             {
  25.                 int n = board[i][j] - '1';
  26.                 if (flag[n]) return false;
  27.                 else flag[n] = true;
  28.             }
  29.         }
  30.         memset(flag, 0, sizeof(flag));
  31.     }
  32.     
  33.     /* boxes */
  34.     int ii, jj;
  35.     for (ii = 0; ii < boardRowSize; ii += 3)
  36.     {
  37.         for (jj = 0; jj < boardColSize; jj += 3)
  38.         {
  39.             for (i = 0; i < 3; i++)
  40.             {
  41.                 for (j = 0; j < 3; j++)
  42.                 {
  43.                     if (board[i + ii][j + jj] != '.')
  44.                     {
  45.                         int n = board[i + ii][j + jj] - '1';
  46.                         if (flag[n]) return false;
  47.                         flag[n] = true;
  48.                     }
  49.                 }
  50.             }
  51.             memset(flag, 0, sizeof(flag));
  52.         }
  53.     }
  54.     return true;
  55. }


阅读(255) | 评论(0) | 转发(0) |
0

上一篇:Count Primes

下一篇:String to Integer (atoi)

给主人留下些什么吧!~~