Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1.  
  2. package land_monitoring_gui;
  3.  
  4.  
  5. public class Land_Monitoring {
  6.     private static int[] b = new int[8];
  7.   private static int s = 0;
  8.  
  9.   static boolean unsafe(int y) {
  10.     int x = b[y];
  11.     for (int i = 1; i <= y; i++) {
  12.       int t = b[y - i];
  13.       if (t == x ||
  14.           t == x - i ||
  15.           t == x + i) {
  16.         return true;
  17.       }
  18.     }
  19.  
  20.     return false;
  21.   }
  22.  
  23.   public static void putboard() {
  24.     System.out.println("\n\nSolution " + (++s));
  25.     for (int y = 0; y < 8; y++) {
  26.       for (int x = 0; x < 8; x++) {
  27.         System.out.print((b[y] == x) ? "|Q" : "|_");
  28.       }
  29.       System.out.println("|");
  30.     }
  31.   }
  32.     public static void main(String[] args) {
  33.         int y = 0;
  34.     b[0] = -1;
  35.     while (y >= 0) {
  36.       do {
  37.         b[y]++;
  38.       } while ((b[y] < 8) && unsafe(y));
  39.       if (b[y] < 8) {
  40.         if (y < 7) {
  41.           b[++y] = -1;
  42.         } else {
  43.           putboard();
  44.         }
  45.       } else {
  46.         y--;
  47.       }
  48.     }
  49.   }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement