From 56d06a80efebb7645b70da5cf71dc2019654614e Mon Sep 17 00:00:00 2001 From: wheelman1995 Date: Tue, 11 Sep 2018 12:50:57 +0300 Subject: [PATCH] add lesson5 homework --- lesson5/src/Item.java | 21 ++++++++ lesson5/src/Knight.java | 77 ++++++++++++++++++++++++++++++ lesson5/src/Main.java | 14 ++++++ lesson5/src/Rucksack.java | 21 ++++++++ lesson5/src/TowerOfHanoi.java | 48 +++++++++++++++++++ lesson5/src/UnboundedRucksack.java | 25 ++++++++++ 6 files changed, 206 insertions(+) create mode 100644 lesson5/src/Item.java create mode 100644 lesson5/src/Knight.java create mode 100644 lesson5/src/Main.java create mode 100644 lesson5/src/Rucksack.java create mode 100644 lesson5/src/TowerOfHanoi.java create mode 100644 lesson5/src/UnboundedRucksack.java diff --git a/lesson5/src/Item.java b/lesson5/src/Item.java new file mode 100644 index 0000000..f56414b --- /dev/null +++ b/lesson5/src/Item.java @@ -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; + } + +} diff --git a/lesson5/src/Knight.java b/lesson5/src/Knight.java new file mode 100644 index 0000000..0e29e20 --- /dev/null +++ b/lesson5/src/Knight.java @@ -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(); + } + } + +} diff --git a/lesson5/src/Main.java b/lesson5/src/Main.java new file mode 100644 index 0000000..e1a8dbf --- /dev/null +++ b/lesson5/src/Main.java @@ -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)); + } +} diff --git a/lesson5/src/Rucksack.java b/lesson5/src/Rucksack.java new file mode 100644 index 0000000..169f1ea --- /dev/null +++ b/lesson5/src/Rucksack.java @@ -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); + } + } +} diff --git a/lesson5/src/TowerOfHanoi.java b/lesson5/src/TowerOfHanoi.java new file mode 100644 index 0000000..f5a9119 --- /dev/null +++ b/lesson5/src/TowerOfHanoi.java @@ -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(); + } + +} diff --git a/lesson5/src/UnboundedRucksack.java b/lesson5/src/UnboundedRucksack.java new file mode 100644 index 0000000..9bf8130 --- /dev/null +++ b/lesson5/src/UnboundedRucksack.java @@ -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)); + } + } +} + +