Compare commits
9 Commits
4286d01786
...
main
Author | SHA1 | Date | |
---|---|---|---|
80253dc3e8 | |||
|
8a8589a25d | ||
|
b06c0ffcab | ||
|
c7aa18990e | ||
|
2a7ba7393c | ||
|
318025d3df | ||
|
00200af26b | ||
|
9ff0ded7ea | ||
|
9aaeaeed5b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -29,3 +29,4 @@ bin/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
/Medien/Bilder & Co/Goldfrosch.pptx
|
/Medien/Bilder & Co/Goldfrosch.pptx
|
||||||
/Medien/Bilder & Co/Goldfrosch.xlsx
|
/Medien/Bilder & Co/Goldfrosch.xlsx
|
||||||
|
/src/HZ/
|
||||||
|
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -1,5 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@@ -8,7 +8,7 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
|
|||||||
## Bisher vollständig vorhanden:
|
## Bisher vollständig vorhanden:
|
||||||
- Vorlesung 3 komplett
|
- Vorlesung 3 komplett
|
||||||
- Vorlesung 4 komplett
|
- Vorlesung 4 komplett
|
||||||
- Vorlesung 5 Aufgabe 1 & 3 von 3
|
- Vorlesung 5 komplett
|
||||||
- Vorlesung 6 komplett
|
- Vorlesung 6 komplett
|
||||||
- Vorlesung 7 komplett
|
- Vorlesung 7 komplett
|
||||||
- Vorlesung 8 komplett
|
- Vorlesung 8 komplett
|
||||||
@@ -16,8 +16,6 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
|
|||||||
- Vorlesung 10 komplett
|
- Vorlesung 10 komplett
|
||||||
|
|
||||||
## Angefangene unfertige Lösungen:
|
## Angefangene unfertige Lösungen:
|
||||||
- Damenproblem: VL 5 Aufgabe 2
|
|
||||||
<br>Nicht die gewünschte rekursive Backtracking-Implementierung
|
|
||||||
- JavaDoc: VL 9 Aufgabe 1
|
- JavaDoc: VL 9 Aufgabe 1
|
||||||
<br>Das Tool mag mich nicht
|
<br>Das Tool mag mich nicht
|
||||||
|
|
||||||
|
@@ -9,7 +9,7 @@ public class aufg3 {
|
|||||||
// =====================
|
// =====================
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
int MAXIMUM = 10_000_000; // takes about 13 seconds(!!)
|
int MAXIMUM = 100_000_000; // takes about 4 seconds(!!)
|
||||||
int MAX_PER_ROW = 20;
|
int MAX_PER_ROW = 20;
|
||||||
|
|
||||||
boolean[] primes = new boolean[MAXIMUM];
|
boolean[] primes = new boolean[MAXIMUM];
|
||||||
@@ -17,7 +17,7 @@ public class aufg3 {
|
|||||||
Arrays.fill(primes, true);
|
Arrays.fill(primes, true);
|
||||||
|
|
||||||
for(int i=2; i < MAXIMUM * 0.5; i++){
|
for(int i=2; i < MAXIMUM * 0.5; i++){
|
||||||
for(int j=2; j < ((MAXIMUM*1.0)/i); j++){
|
for(int j=i; j < ((MAXIMUM*1.0)/i); j++){
|
||||||
|
|
||||||
int index = i * j;
|
int index = i * j;
|
||||||
|
|
||||||
|
@@ -21,12 +21,12 @@ public class aufg1 {
|
|||||||
result = 0;
|
result = 0;
|
||||||
} else if (number < 2) {
|
} else if (number < 2) {
|
||||||
result = 1;
|
result = 1;
|
||||||
}else if (number == 10) { // Optimization1
|
// }else if (number == 10) { // Optimization1
|
||||||
result = 89;
|
// result = 89;
|
||||||
}else if (number == 20) { // Optimization2
|
// }else if (number == 20) { // Optimization2
|
||||||
result = 10946;
|
// result = 10946;
|
||||||
}else if (number == 30) { // Optimization3
|
// }else if (number == 30) { // Optimization3
|
||||||
result = 1346269;
|
// result = 1346269;
|
||||||
}else {
|
}else {
|
||||||
result = fibonacciRec(number-1) + fibonacciRec(number-2);
|
result = fibonacciRec(number-1) + fibonacciRec(number-2);
|
||||||
}
|
}
|
||||||
@@ -34,11 +34,27 @@ public class aufg1 {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int fiboTailRec(int number){
|
||||||
|
return helper(++number, new int[] {0,1});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int helper(int number, int[] result){
|
||||||
|
if (number < 0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (number < 2){
|
||||||
|
return result[number];
|
||||||
|
}
|
||||||
|
|
||||||
|
return helper(number-1, new int[] {result[1], result[0] + result[1]});
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] agrs){
|
public static void main(String[] agrs){
|
||||||
// Fib(47) causes an Integer Overflow
|
// Fib(47) causes an Integer Overflow
|
||||||
for (int i=0; i<46; i++) {
|
for (int i=0; i<46; i++) {
|
||||||
System.out.println("i " + i + ": " + fibonacciIter(i));
|
System.out.println("i " + i + ": " + fibonacciIter(i));
|
||||||
System.out.println("i " + i + ": " + fibonacciRec(i));
|
System.out.println("i " + i + ": " + fibonacciRec(i));
|
||||||
|
System.out.println("i " + i + ": " + fiboTailRec(i));
|
||||||
System.out.println("==========");
|
System.out.println("==========");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,20 +3,10 @@ package part5.aufg2;
|
|||||||
public class Demo {
|
public class Demo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Queenproblem Q = new Queenproblem();
|
Queenproblem Q = new Queenproblem();
|
||||||
Q.setField(new int[]{2,4,6,8,1,3,5,0});
|
Q.solve();
|
||||||
Q.print();
|
System.out.println("====================================\n");
|
||||||
System.out.println("============= ALL");
|
Q.setField(new int[]{0,0,0,0,0,0,0,0});
|
||||||
System.out.println(Q.isPossible(7,8));
|
Q.solve();
|
||||||
System.out.println("============= Horiz");
|
|
||||||
System.out.println(Q.checkHorizontal(7));
|
|
||||||
System.out.println("============= Vert");
|
|
||||||
System.out.println(Q.checkVertical(8));
|
|
||||||
System.out.println("============= Rising");
|
|
||||||
System.out.println(Q.checkRisingDiagonal(7,8));
|
|
||||||
System.out.println("============= Falling");
|
|
||||||
System.out.println(Q.checkFallingDiagonal(7,8));
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,15 +6,20 @@ public class Queenproblem {
|
|||||||
// 0 means empty
|
// 0 means empty
|
||||||
// 1-8 is the y position of the Queen
|
// 1-8 is the y position of the Queen
|
||||||
int[] field;
|
int[] field;
|
||||||
int[][] field2D;
|
|
||||||
public Queenproblem(){
|
public Queenproblem(){
|
||||||
this.field = new int[8];
|
this.field = new int[8];
|
||||||
|
makeBoardEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setField(int[] field) {
|
public void setField(int[] field) {
|
||||||
this.field = field;
|
this.field = field;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void makeBoardEmpty(){
|
||||||
|
this.setField(new int[] {0,0,0,0,0,0,0,0});
|
||||||
|
}
|
||||||
|
|
||||||
private int[][] make2D(){
|
private int[][] make2D(){
|
||||||
int[][] field = new int[8][8];
|
int[][] field = new int[8][8];
|
||||||
|
|
||||||
@@ -26,10 +31,7 @@ public class Queenproblem {
|
|||||||
try {
|
try {
|
||||||
int yPosition = this.field[i];
|
int yPosition = this.field[i];
|
||||||
field[yPosition-1][i] = 1; // -1 to handle 0 as empty and 8 as lowest possible with actual index 7
|
field[yPosition-1][i] = 1; // -1 to handle 0 as empty and 8 as lowest possible with actual index 7
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){/* System.out.println("Ignored Index Error while making it 2D") */}
|
||||||
;
|
|
||||||
//System.out.println("Ignored Index Error while making it 2D");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return field;
|
return field;
|
||||||
@@ -44,10 +46,7 @@ public class Queenproblem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkVertical(int col){
|
public boolean checkVertical(int col){
|
||||||
if (this.field[col-1] != 0){
|
return this.field[col - 1] == 0;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkHorizontal(int row){
|
public boolean checkHorizontal(int row){
|
||||||
@@ -77,9 +76,7 @@ public class Queenproblem {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError */}
|
||||||
; // Skip Iteration with IndexError
|
|
||||||
}
|
|
||||||
|
|
||||||
// cannot be in the same try block.
|
// cannot be in the same try block.
|
||||||
// Exceptions in the + direction stop execution of - prematurely
|
// Exceptions in the + direction stop execution of - prematurely
|
||||||
@@ -89,9 +86,7 @@ public class Queenproblem {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError*/ }
|
||||||
; // Skip Iteration with IndexError
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -106,59 +101,40 @@ public class Queenproblem {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){}
|
||||||
;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
if (this.field[col-1-1] != 0) {
|
if (this.field[col-1-i] != 0) {
|
||||||
if (this.field[col - 1 - i] == row + 1) {
|
if (this.field[col-1-i] == row+i) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){}
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public boolean isPossible(int row, int col){
|
public boolean isPossible(int row, int col){
|
||||||
|
if(!checkHorizontal(row)){
|
||||||
if(checkHorizontal(row) == false){
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!checkVertical(col)) {
|
||||||
if (checkVertical(col) == false) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!checkFallingDiagonal(row, col)){
|
||||||
// Diagonal \
|
|
||||||
if (checkFallingDiagonal(row, col) == false){
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!checkRisingDiagonal(row, col)){
|
||||||
|
|
||||||
if (checkRisingDiagonal(row, col) == false){
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void solve(int queensPlaced){
|
public void solve(){
|
||||||
field2D = make2D();
|
|
||||||
if (queensPlaced == 6){
|
|
||||||
System.out.println("solved 6");
|
|
||||||
}
|
|
||||||
for (int col = 1; col < 9; col++) {
|
for (int col = 1; col < 9; col++) {
|
||||||
if(col == 8) {
|
|
||||||
System.out.println("solving the last one");
|
|
||||||
}
|
|
||||||
if (this.field[col-1] == 0) {
|
if (this.field[col-1] == 0) {
|
||||||
for (int row = 1; row < 9; row++) {
|
for (int row = 1; row < 9; row++) {
|
||||||
if (isPossible(row,col)) {
|
if (isPossible(row,col)) {
|
||||||
this.field[col-1] = row;
|
this.field[col-1] = row;
|
||||||
queensPlaced++;
|
this.solve();
|
||||||
this.solve(queensPlaced);
|
|
||||||
queensPlaced--;
|
|
||||||
this.field[col-1] = 0;
|
this.field[col-1] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user