1
0

Dateien nach "/" hochladen

8 Damen und Fibonacci
This commit is contained in:
Matti Bos 2023-12-04 15:04:30 +00:00
parent c76da3f3aa
commit ce3f868383

151
Main.java Normal file
View File

@ -0,0 +1,151 @@
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static long Fibonacci_Iter(int num){
long smol = 1;
long big = 1;
long temp;
while (1 < num){
temp = smol;
smol = big;
big = big + temp;
num--;
}
return big;
}
public static long Fibonacci_Rec(int num){
// Inputqualität
if(num < 0){
System.out.println("Ungültiger Input");
return 0;
}
if(num < 2){
return 1;
}
return Fibonacci_Rec(num-2) + Fibonacci_Rec(num-1);
}
public static long Fibonacci_Round(int num){
long result = 1;
if(num==0 || num==1){return 1;}
for(int i=1; i<num; i++){result = Math.round(result*1.61803398875);}
return result;
}
public static void QueensPlace(int field[][],int row, int col){
// block spaces
for (int i=0; i<8; i++){
// block row and col
field[i][col]=3;
field[row][i]=3;
// block diagonals
// Left 2 Right, Top 2 Bottom
if (row - col + i < 8 && row - col + i >= 0) {
field[row-col + i][i]=3;
}
// Left 2 Right, Bottom 2 Top
if(row+i < 8 && row+i >=0 && col-i < 8 && col-i >=0){
field[row+i][col-i]=3;
}
if(row-i < 8 && row-i >=0 && col+i < 8 && col+i >=0){
field[row-i][col+i]=3;
}
}
// place queen
field[row][col]=1;
}
public static boolean QueensFree(int field[][], int row, int col){
return field[row][col] == 0;
}
public static void QueensPrint(int field[][]){
// print field
System.out.println("---------------");
for(int i=0; i<field.length; i++){
for(int j=0; j<field[i].length; j++) {
System.out.print(field[i][j]+" ");
}
System.out.println();
}
System.out.println("---------------");
System.out.println();
}
public static int[][] QueensSetup(int rows, int cols){
int field[][] = new int[rows][cols];
for(int i = 0; i < rows-1; i++){
for(int j = 0; j < cols-1; j++){
field[i][j]=0;
}
}
return field;
}
public static void QueensMain(){
int rows=8;
int cols=8;
int start_queens=8;
int queens = start_queens;
int field[][] = QueensSetup(rows,cols);
int wrong[][] = new int[8][8];
while(queens>0){
boolean full = true;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(QueensFree(field,i,j)){
QueensPlace(field,i,j);
queens--;
full = false;
}
}
}
System.out.println("Lösungsversuch: ");
QueensPrint(field);
if(full){
// current Queens as wrong for next time
for (int i=0; i<8; i++){
for(int j=0; j<8; j++){
if(field[i][j]==1){
wrong[i][j]=3;
}
}
}
// reset
field = QueensSetup(rows,cols);
queens = start_queens;
// block earlier wrongs
for(int i = 0; i<8; i++){
for(int j = 0; j<8; j++){
if(wrong[i][j]==3){
field[i][j]=3;
}
}
}
System.out.println("New Starting field: ");
QueensPrint(field);
// aktuelle Positionen d Queens merken und sperren, rest reset
}
}
System.out.println("Finale Lösung: ");
QueensPrint(field);
}
public static void main(String[] args) {
/*for(int i=0; i<4; i++) {
System.out.println("i= "+i);
System.out.println("Iter : " + Fibonacci_Iter(i));
System.out.println("Rec : " + Fibonacci_Rec(i));
System.out.println("Round: " + Fibonacci_Round(i));
System.out.println();
}*/
// ---------------------------------------------------------
QueensMain();
}
}