Visit our sponsor Five Planet Juices  
Home
Microsoft Interview Process
Microsoft HR Questions
Technical Questions
Puzzles/Riddles
Resume Tips and Template
Discuss
Question to Interviewer
Interview Tips
Term Of Use
Site Feedback


N-Queen Problem.

 

Comments:

Solution in C#
By kp.zeus on Saturday, July 05, 2008 (UMST)
public class NQueen { public static void DisplaySolutions(int sizeOfBoard) { Console.WriteLine("N Queens Solutions For Size : " + sizeOfBoard); List<Square[]> solutions = GetSolutions(sizeOfBoard); Console.WriteLine("No. of Solns : " + solutions.Count); foreach (Square[] solution in solutions) { foreach (Square square in solution) { Console.Write("{" + square.X + "," + square.Y + "}"); } Console.WriteLine(); } Console.WriteLine(); } public static List<Square[]> GetSolutions(int sizeOfBoard) { List<Square[]> solutions = new List<Square[]>(); //1,2,3 Not Possible if (sizeOfBoard > 3) { int x = 0; int y = 0; while (x < sizeOfBoard) { while (y < sizeOfBoard) { List<Square[]> subset = CheckFromSquare(sizeOfBoard, x, y); foreach (Square[] solution in subset) { if (!ContainsSolution(solutions,solution)) solutions.Add(solution); } y++; } x++; } } return solutions; } private static List<Square[]> CheckFromSquare(int sizeOfBoard, int x, int y) { List<Square[]> solutions = new List<Square[]>(); Square[] solution = new Square[sizeOfBoard]; bool[][] squares = GetNewBoard(sizeOfBoard); Dictionary<Square, List<Square>> failedPaths = new Dictionary<Square, List<Square>>(); int i = 0, j = 0, queenCount = 0; int count = 0, totalChecks = (int)Math.Pow(sizeOfBoard, sizeOfBoard); totalChecks = sizeOfBoard * sizeOfBoard; totalChecks = 1000; while (count < totalChecks) { count++; i = 0; j = 0; squares = GetNewBoard(sizeOfBoard); //Set Start Position To First Queen queenCount = 1; squares[x][y] = true; solution[queenCount - 1] = new Square(x, y); while (i < sizeOfBoard) { while (j < sizeOfBoard && queenCount <= sizeOfBoard) { if (Safe(squares, i, j)) { if (failedPaths.ContainsKey(solution[queenCount - 1]) && failedPaths[solution[queenCount - 1]].Contains(new Square(i, j)) ) { } else { queenCount++; solution[queenCount - 1] = new Square(i, j); squares[i][j] = true; break; } //if (queenCount == (minQueenCount + 1) && IsFailedPath(failedPaths, solution)) //{ // solution[queenCount - 1] = null; // queenCount--; //} } j++; } j = 0; i++; } if (queenCount == sizeOfBoard) { solutions.Add(solution); } else { if (queenCount > 1) { if (!failedPaths.ContainsKey(solution[queenCount - 2])) failedPaths.Add(solution[queenCount - 2], new List<Square>()); if (!failedPaths[solution[queenCount - 2]].Contains(solution[queenCount - 1])) { failedPaths[solution[queenCount - 2]].Add(solution[queenCount - 1]); } } } solution = new Square[sizeOfBoard]; } //if (queenCount == sizeOfBoard) // return solution; //else //{ // //if (history.Count < ((sizeOfBoard - 1) * sizeOfBoard)) // //{ // // history.Add(solution); // // return CheckFromSquare(sizeOfBoard, x, y, history); // //} //} return solutions; } private static bool SameSolution(Square[] a, Square[] b) { int i = 0; while ((a[i] == null && b[i] == null) || a[i].Equals(b[i])) { i++; } if (i == a.Length) return true; return false; } private static bool ContainsSolution(List<Square[]> solutions, Square[] solution) { int i = 0; foreach (Square[] element in solutions) { while (i < element.Length && ( (element[i] == null && solution[i] == null) || element[i].Equals(solution[i]) ) ) { i++; } if (i == element.Length) return true; i = 0; } return false; } private static bool Safe(bool[][] squares, int x, int y) { if ( HorizontalSafe(squares, x, y) && VerticalSafe(squares, x, y) && DiagonalSafe(squares, x, y) ) return true; else return false; } private static bool HorizontalSafe(bool[][] squares, int x, int y) { for (int i = 0; i < squares.Length; i++) { if (squares[x][i]) return false; } return true; } private static bool VerticalSafe(bool[][] squares, int x, int y) { for (int i = 0; i < squares.Length; i++) { if (squares[i][y]) return false; } return true; } private static bool DiagonalSafe(bool[][] squares, int x, int y) { int i = x; int j = y; //x increases, y increases while (i < squares.Length && j < squares.Length) { if (squares[i][j]) return false; i++; j++; } //x decreases, y increases i = x; j = y; while (i > -1 && j < squares.Length) { if (squares[i][j]) return false; i--; j++; } //x increases, y decreases i = x; j = y; while (i < squares.Length && j > -1) { if (squares[i][j]) return false; i++; j--; } //x decreases, y decreases i = x; j = y; while (i > -1 && j > -1) { if (squares[i][j]) return false; i--; j--; } return true; } private static bool[][] GetNewBoard(int sizeOfBoard) { bool[][] squares = new bool[sizeOfBoard][]; for (int i = 0; i < sizeOfBoard; i++) { squares[i] = new bool[sizeOfBoard]; } return squares; } } public class Square { int x; public int X { get { return x; } } int y; public int Y { get { return y; } } public Square(int x, int y) { this.x = x; this.y = y; } public override bool Equals(object obj) { Square entity = obj as Square; if (entity != null && entity.X == this.X && entity.Y == this.Y) return true; else return false; } public override int GetHashCode() { return (this.X * 10) + this.y; } }

Reply to this Comment
 

Add Your Comment

New Articles
  • Combinations in a character array.
    Write a program that takes input a char array and outputs all the combinations of the characters in the character array.
    Example: consier char array {'a','b','c'}
    the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'.


  • N-Queen Problem.
    Write a Program to solve N-Queen Problem.

  • Reverse a string
    Reverse a string using recursive function

  •  

    Most Popular Articles
  • Crazy Guy On The Airplane
    A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that flight.

  • What are your greatest weaknesses
    Beware - this is an eliminator question, designed to shorten the candidate list. Any admission of a weakness or fault will earn you an “A” for honesty, but an “F” for the interview.

  • Question: Tell me about yourself.
    Many candidates, unprepared for the question, skewer themselves by rambling, recapping their life story, delving into ancient work history or personal matters.

  •  

    New Posts

  • My News Topic 8
    Posted by evashy on Friday, September 05, 2008 (UMST)

  • 3StandwOnsIt
    Posted by undondorade on Wednesday, September 03, 2008 (UMST)

  • 2SOnKeysOns
    Posted by undondorade on Tuesday, September 02, 2008 (UMST)



  •