James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 166 , comments - 1431 , trackbacks - 0

My Links

News

Welcome to my blog! I'm a Sr. Software Development Engineer in the Seattle area, who has been performing C++/C#/Java development for over 20 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

Blogs I Read

Follow BlkRabbitCoder on Twitter

Tag Cloud

Archives

Post Categories

.NET

CSharp

Little Wonders

Little Wonders

vNext

Little Puzzlers–Validate a Sudoku Board

I like to keep my brain sharp by working on programming puzzlers. On off weeks I'm going to start posting programming puzzlers I've collected over the years. Hopefully you'll find them as entertaining as I do.

This one is a more straight-forward puzzler.  There’s no gotcha or “ah-hah” moment in solving this, it’s just an exercise in coding.  I’ll try to vary up the difficulty level of problems so they’re not all easy and not all brain-busters…

The Problem

Given a 2d 9 x 9 array of char representing an in-progress Sudoku board, validate that the board currently has no errors.  The​ board may be partially filled and may be unsolvable, the challenge is to simply determine if there are any errors with the solution so far.​

A Sudoku board has no errors if there are no numbers duplicated in the same row, or in the same column, or in the same 3x3 cube (as shown below by the heavy lines dividing the board into 9 equal 3x3 cubes).

Empty cells will be represented by a space (' ') otherwise, the cells will have the character representation of the numbers '1' thru '9'.

For example, this board:

250px-Sudoku-by-L2G-20050714_svg

Would be represented by the 2d array:

   1: var board = new char[9,9] 
   2:  {
   3:   {'5', '3', ' ', ' ', '7', ' ', ' ', ' ', ' '},
   4:   {'6', ' ', ' ', '1', '9', '5', ' ', ' ', ' '},
   5:   {' ', '9', '8', ' ', ' ', ' ', ' ', '6', ' '},
   6:   {'8', ' ', ' ', ' ', '6', ' ', ' ', ' ', '3'},
   7:   {'4', ' ', ' ', '8', ' ', '3', ' ', ' ', '1'},
   8:   {'7', ' ', ' ', ' ', '2', ' ', ' ', ' ', '6'},
   9:   {' ', '6', ' ', ' ', ' ', ' ', '2', '8', ' '},
  10:   {' ', ' ', ' ', '4', '1', '9', ' ', ' ', '5'},
  11:   {' ', ' ', ' ', ' ', '8', ' ', ' ', '7', '9'},
  12:  };
Again, the goal is to simply state if there are any errors in the Sudoku board as it stands, not to determine if the puzzle has a solution or is solvable.

Spoiler Alert!

Fair Warning: there may be discussion of the problem and potential solutions posted in the comments below. Read at your own risk.

Print | posted on Tuesday, May 19, 2015 7:23 PM |

Powered by: