Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lesson5/src/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Item {

String name;
int value;
float weight;
float volume;

public Item(String name, int value, float weight) {
this.name = name;
this.value = value;
this.weight = weight;
}

public Item(String name, int value, float weight, float volume) {
this.name = name;
this.value = value;
this.weight = weight;
this.volume = volume;
}

}
77 changes: 77 additions & 0 deletions lesson5/src/Knight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
public class Knight {
private static int Y = 8;
private static int X = 8;
private static int[][] board = new int[Y][X];
private static int move = 0;
private static int curX = -2, curY = -1;

public static void main(String[] args) {
initBoard();
knight();
printBoard();
}

private static boolean checkFull() {
for (int i = 0; i < Y; i++) {
for (int j = 0; j < X; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}

private static boolean checkMove(int curY, int curX, int y, int x) {
if ((Math.abs(x - curX) == 2 && Math.abs(y - curY) == 1) || (Math.abs(x - curX) == 1 && Math.abs(y - curY) == 2)) {
return board[y][x] == 0;
}
return false;
}

private static boolean knight() {
if (checkFull()) return true;

for (int i = 0; i < Y; i++) {
for (int j = 0; j < X; j++) {
if (checkMove(curY, curX, i, j)) {
board[i][j] = ++move;
int prevY = curY;
int prevX = curX;
curY = i;
curX = j;

// printBoard();
// System.out.println();
if (knight()) return true;

move--;
board[i][j] = 0;
curY = prevY;
curX = prevX;
}
}
}

return false;
}


private static void initBoard() {
for (int i = 0; i < Y; i++) {
for (int j = 0; j < X; j++) {
board[i][j] = 0;
}
}
}

private static void printBoard() {
for (int i = 0; i < Y; i++) {
for (int j = 0; j < X; j++) {
System.out.printf("%3d", board[i][j]);
}
System.out.println();
}
}

}
14 changes: 14 additions & 0 deletions lesson5/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Main {
public static void main(String[] args) {

Item silver = new Item("silver", 2500, 2.0f, 0.002f);
Item gold = new Item("gold", 3000, 0.3f, 0.025f);
Item bronze = new Item("bronze", 1800, 0.2f, 0.015f);

Item[] items = {gold, silver, bronze};

System.out.println("max value: " + Rucksack.solve(items, 2.2f));

System.out.println("max value: " + UnboundedRucksack.solve(items, 25, 0.25f));
}
}
21 changes: 21 additions & 0 deletions lesson5/src/Rucksack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Rucksack {

public static int solve(Item[] items, float maxWeight) {
return solve(items, maxWeight, items.length - 1);
}

private static int solve(Item[] items, float maxWeight, int n) {
if (n == -1 || maxWeight == 0)
return 0;

if (items[n].weight > maxWeight)
return solve(items, maxWeight, n - 1);

else {
int temp1 = solve(items, maxWeight, n - 1);
int temp2 = items[n].value + solve(items, maxWeight - items[n].weight, n - 1);

return Math.max(temp1, temp2);
}
}
}
48 changes: 48 additions & 0 deletions lesson5/src/TowerOfHanoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class TowerOfHanoi {
private static int[][] tower ={{8,7,6,5,4,3,2,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
private static int iterations = 0;
private static int moves = 0;

public static void main(String[] args) {
printTowers();
tower(8, tower[0], tower[1], tower[2]);
System.out.println("iterations: " + iterations);
System.out.println("moves: " + moves);
}

private static int findTop(int[] arr) {
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) return i;
}
return -1;
}

private static void tower(int n, int[] src, int[] temp, int[] dst) {
iterations++;
if (n == 1) {
int dstTop = findTop(dst);
int srcTop = findTop(src);
dst[dstTop + 1] = src[srcTop];
src[srcTop] = 0;
printTowers();
moves++;

} else {
tower(n - 1, src, dst, temp);
tower(1, src, temp, dst);
tower(n - 1, temp, src, dst);


}


}

private static void printTowers() {
for (int i = tower[0].length - 1; i >= 0 ; i--) {
System.out.println(tower[0][i] + " | " + tower[1][i] + " | " + tower[2][i]);
}
System.out.println();
}

}
25 changes: 25 additions & 0 deletions lesson5/src/UnboundedRucksack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class UnboundedRucksack {

public static int solve(Item[] items, int maxWeight, float maxVolume) {
return solve(items, maxWeight, maxVolume, items.length - 1);
}

private static int solve(Item[] items, float maxWeight, float maxVolume, int n) {
if (n == -1 || maxWeight <= 0 || maxVolume <= 0)
return 0;


if (items[n].weight > maxWeight || items[n].volume > maxVolume)
return solve(items, maxWeight, maxVolume, n - 1);

else {
int temp1 = solve(items, maxWeight, maxVolume, n - 1);
int temp2 = items[n].value + solve(items, maxWeight - items[n].weight, maxVolume - items[n].volume, n);
int temp3 = items[n].value + solve(items, maxWeight - items[n].weight, maxVolume - items[n].volume, n - 1);

return Math.max(temp1, Math.max(temp2, temp3));
}
}
}