Advertisement
Guest User

Ich bim kuhl kappa

a guest
Apr 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package de.konstnsti.main;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ndame
  6. {
  7.  
  8.  
  9.     public static void main(String[] args)
  10.     {
  11.  
  12.  
  13.  
  14.  
  15.         Scanner s = new Scanner( System.in );
  16.  
  17.         System.out.print( "Wie viele Damen sollen vorhanden sein? : " );
  18.         System.out.println();
  19.         int n = s.nextInt();
  20.         int[] d = damen( n );
  21.         raus( d );
  22.  
  23.     }
  24.  
  25.  
  26.     public static int[] damen(int n)
  27.     {
  28.         int[] d = new int[n];
  29.         damen( d, 0 );
  30.         return d;
  31.     }
  32.  
  33.  
  34.     public static boolean damen(int[] d2, int col)
  35.     {
  36.         if( col == d2.length )
  37.             return true;
  38.  
  39.         for( int reihe = 0; reihe < d2.length; reihe++ )
  40.         {
  41.             if( sicher( d2, reihe, col ) )
  42.             {
  43.                 d2[col] = reihe;
  44.  
  45.                 if( damen( d2, col + 1 ) )
  46.  
  47.                     return true;
  48.             }
  49.         }
  50.         return false;
  51.     }
  52.  
  53.  
  54.     public static boolean sicher(int[] d2, int reihe, int col)
  55.     {
  56.         int hoch = reihe;
  57.         int runter = reihe;
  58.         boolean istSicher = true;
  59.         for( int h = col - 1; istSicher && h >= 0; h-- )
  60.         {
  61.             int r = d2[h];
  62.             istSicher = r != reihe && r != ++hoch && r != --runter;
  63.         }
  64.         return istSicher;
  65.     }
  66.  
  67.  
  68.  
  69.     public static void raus(int[] d2)
  70.     {
  71.  
  72.         for( int i = 0; i < d2.length; i++ )
  73.  
  74.             System.out.printf( "%4d", d2[i] + 1 );
  75.  
  76.  
  77.     }
  78.  
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement