Advertisement
ptrelford

NQueens benchmark

Jul 9th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class NQueens {
  5.  
  6.     public class Position {
  7.         int x;
  8.         int y;
  9.  
  10.         public Position(int i, int j) {
  11.             x = i;
  12.             y = j;
  13.         }
  14.  
  15.         public bool canPlace(Position other) {
  16.             return x != other.x && y != other.y && Math.Abs(x - other.x) != Math.Abs(y - other.y);
  17.         }
  18.  
  19.         public String toString() {
  20.             return "(" + x + "," + y + ")";
  21.         }
  22.     }
  23.  
  24.     public static Position position(int i, int j) {
  25.         return new Position(i, j);
  26.     }
  27.  
  28.     public bool isValid(Position prospect, List<Position> solution) {
  29.         foreach (Position position in solution) {
  30.             if (!prospect.canPlace(position)) {
  31.                 return false;
  32.             }
  33.         }
  34.         return true;
  35.     }
  36.  
  37.     public List<Position> nextColPositions(int col, int n, List<Position> solution) {
  38.         List<Position> ret = new List<Position>();
  39.         Position temp;
  40.         for (int i = 0; i < n; i++) {
  41.             temp = position(i, col);
  42.             if (isValid(temp, solution)) {
  43.                 ret.Add(temp);
  44.             }
  45.         }
  46.         return ret;
  47.     }
  48.  
  49.     public List<List<Position>> solve(int col, int n, List<List<Position>> solutions) {
  50.         List<List<Position>> result = new List<List<Position>>();
  51.         if (col >= n) {
  52.             return solutions;
  53.         } else {
  54.             List<Position> prospects;
  55.             List<Position> temp = null;
  56.             foreach (List<Position> solution in solutions) {
  57.                 prospects = nextColPositions(col, n, solution);
  58.                 foreach (Position p in prospects) {
  59.                     temp = new List<Position>(solution);
  60.                     temp.Add(p);
  61.                     result.Add(temp);
  62.                 }
  63.                 solution.Clear();
  64.             }
  65.             solutions.Clear();
  66.             return solve(col + 1, n, result);
  67.         }
  68.     }
  69.  
  70.     public void nqueens(int n) {
  71.         List<List<Position>> input = new List<List<Position>>();
  72.         input.Add(new List<Position>());
  73.         List<List<Position>> solutions = solve(0, n, input);      
  74.         Console.WriteLine("No. of Solutions for n = " + n + " : " + solutions.Count);
  75.     }
  76.  
  77.     public static void Main(string[] args) {
  78.         int n = 13;
  79.         for (int i = 0; i <= n; i++) {
  80.             var timer = System.Diagnostics.Stopwatch.StartNew();
  81.             new NQueens().nqueens(i);
  82.             timer.Stop();
  83.             Console.WriteLine("Time taken(ms) : " + timer.ElapsedMilliseconds);
  84.         }
  85.         Console.WriteLine("Poop");
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement