Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.konstnsti.main;
- import java.util.Scanner;
- public class ndame
- {
- public static void main(String[] args)
- {
- Scanner s = new Scanner( System.in );
- System.out.print( "Wie viele Damen sollen vorhanden sein? : " );
- System.out.println();
- int n = s.nextInt();
- int[] d = damen( n );
- raus( d );
- }
- public static int[] damen(int n)
- {
- int[] d = new int[n];
- damen( d, 0 );
- return d;
- }
- public static boolean damen(int[] d2, int col)
- {
- if( col == d2.length )
- return true;
- for( int reihe = 0; reihe < d2.length; reihe++ )
- {
- if( sicher( d2, reihe, col ) )
- {
- d2[col] = reihe;
- if( damen( d2, col + 1 ) )
- return true;
- }
- }
- return false;
- }
- public static boolean sicher(int[] d2, int reihe, int col)
- {
- int hoch = reihe;
- int runter = reihe;
- boolean istSicher = true;
- for( int h = col - 1; istSicher && h >= 0; h-- )
- {
- int r = d2[h];
- istSicher = r != reihe && r != ++hoch && r != --runter;
- }
- return istSicher;
- }
- public static void raus(int[] d2)
- {
- for( int i = 0; i < d2.length; i++ )
- System.out.printf( "%4d", d2[i] + 1 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement