Last active
September 27, 2016 20:24
-
-
Save nasko90/a6243b5892007cdb5fbb8e52b0cfa515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class GameOfBotles { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.println("How many botles are on the field? "); | |
int n = input.nextInt(); | |
int stepsX = 0; | |
int stepsY = 0; | |
int sum = 0; | |
int minDistance=0; | |
if ((n>9)||(n<0)){ | |
System.out.println("The botles on the field have to be at least one and at most nine !"); | |
} | |
else { | |
System.out.println("Choose the cordinats for each botle - 'x' and 'y' "); | |
int field [][] = new int [n] [2]; | |
for (int row=0; row <field.length; row++){ | |
for (int col=0; col <field[row].length;col++){ | |
System.out.printf("field[%d,%d] = ", row, col); | |
field [row][col] = input.nextInt(); | |
} | |
} | |
java.util.Arrays.sort(field, java.util.Comparator.comparingInt(a -> a[0])); | |
for (int row=0; row <field.length-1; row++){ | |
stepsX = field [row+1][0] - field [row][0]; | |
stepsY = field [row+1][1] - field [row][1]; | |
if (stepsY < 0){ | |
stepsY = stepsY * (-1); | |
} | |
sum = stepsX + stepsY; | |
minDistance += sum; | |
stepsY = 0; | |
stepsX = 0; | |
} | |
System.out.println("The smallest walking distance is: "+minDistance); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment