Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PresentDelivery {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int presents = Integer.parseInt(scanner.nextLine());
- int n = Integer.parseInt(scanner.nextLine());
- String[][]matrix = new String [n][n];
- int rowS = -1;
- int colS = -1;
- int goodKids = 0;
- //read info
- for (int i = 0; i < n; i++) {
- String[] infoRow = scanner.nextLine().split(" ");
- //position Santa and count the good kids;
- for (int j = 0; j < infoRow.length; j++) {
- if (infoRow[j].equals("S")){
- colS = j;
- rowS = i;
- }else if (infoRow[j].equals("V")){
- goodKids++;
- }
- }
- matrix[i]=infoRow;
- }
- String command = scanner.nextLine();
- while (!command.equals("Christmas morning")){
- switch (command){
- case "up":
- //validate row
- if (isValid(rowS-1,n)) {
- //clear spot
- matrix[rowS][colS] = "-";
- rowS--;
- int given = givePresent(rowS, colS, matrix);
- presents -= given;
- }
- break;
- case "down":
- if (isValid(rowS+1,n)) {
- matrix[rowS][colS] = "-";
- rowS++;
- int given = givePresent(rowS, colS, matrix);
- presents -= given;
- }
- break;
- case "left":
- if (isValid(colS-1,n)) {
- matrix[rowS][colS] = "-";
- colS--;
- int given = givePresent(rowS, colS, matrix);
- presents -= given;
- }
- break;
- case "right":
- if (isValid(colS+1,n)) {
- matrix[rowS][colS] = "-";
- colS++;
- int given = givePresent(rowS, colS, matrix);
- presents -= given;
- }
- break;
- }
- matrix[rowS][colS]="S";
- if (presents<=0){
- System.out.println("Santa ran out of presents!");
- break;
- }
- command = scanner.nextLine();
- }
- int disappointedKids = printMatrix(matrix);
- if (disappointedKids==0){
- System.out.printf("Good job, Santa! %d happy nice kid/s.", goodKids);
- }else{
- System.out.printf("No presents for %d nice kid/s.",disappointedKids);
- }
- }
- private static int printMatrix(String[][] matrix) {
- int disappointedKids = 0;
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j].equals("V")){
- disappointedKids++;
- }
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- return disappointedKids;
- }
- private static int cookieCase(int rowS, int colS, String[][] matrix) {
- int prGiven = 0;
- if (isValid(rowS-1,matrix.length)){
- prGiven+=givePresent(rowS-1,colS,matrix);
- prGiven+=luckyNaughty(rowS-1, colS, matrix);
- }
- if (isValid(rowS+1, matrix.length)){
- prGiven+=givePresent(rowS+1,colS,matrix);
- prGiven+=luckyNaughty(rowS+1, colS, matrix);
- }
- if (isValid(colS-1,matrix.length)){
- prGiven+=givePresent(rowS, colS-1, matrix);
- prGiven+=luckyNaughty(rowS, colS-1, matrix);
- }
- if (isValid(colS+1,matrix.length)){
- prGiven+=givePresent(rowS, colS+1,matrix);
- prGiven+=luckyNaughty(rowS, colS+1, matrix);
- }
- return prGiven;
- }
- private static int luckyNaughty(int rowS, int colS, String[][] matrix) {
- if (matrix[rowS][colS].equals("X")){
- matrix[rowS][colS]="-";
- return 1;
- }
- return 0;
- }
- private static int givePresent(int row, int col, String[][]matrix){
- if (matrix[row][col].equals("V")){
- matrix[row][col]="-";
- return 1;
- }else if (matrix[row][col].equals("C")){
- matrix[row][col]="-";
- int n = cookieCase(row, col, matrix);
- return 0;
- }
- return 0;
- }
- private static boolean isValid(int s, int n) {
- return s>=0 && s<n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement