Tuesday, August 26, 2014

Career Cup Chapter 9

Problem 9.9
Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal.
  1. What's the output looks like. (each valid placement is a array, results may put in an vector<int[]>)
  2. recursion: 
    1. base case: all row are successfully placed, either print results or count ways.
    2. otherwise check validity to place queens for the followings rows until complete.
      1. To check the column validity: look all the row we placed already, check whether it is possible to place next row in the intended column(as a parameter to the CheckValidity()).
      2. To check the diagonal validity: make sure the column distance and row distance is different.
  3. Might simple to store the results in a two dimension array.
Here is the source code example from CC150 Answer rewrite in C++