Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PresentDelivery {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int presents = Integer.parseInt(scanner.nextLine());
  8. int n = Integer.parseInt(scanner.nextLine());
  9. String[][]matrix = new String [n][n];
  10. int rowS = -1;
  11. int colS = -1;
  12. int goodKids = 0;
  13. //read info
  14. for (int i = 0; i < n; i++) {
  15. String[] infoRow = scanner.nextLine().split(" ");
  16.  
  17. //position Santa and count the good kids;
  18. for (int j = 0; j < infoRow.length; j++) {
  19. if (infoRow[j].equals("S")){
  20. colS = j;
  21. rowS = i;
  22. }else if (infoRow[j].equals("V")){
  23. goodKids++;
  24. }
  25. }
  26. matrix[i]=infoRow;
  27. }
  28.  
  29. String command = scanner.nextLine();
  30. while (!command.equals("Christmas morning")){
  31. switch (command){
  32. case "up":
  33. //validate row
  34. if (isValid(rowS-1,n)) {
  35. //clear spot
  36. matrix[rowS][colS] = "-";
  37. rowS--;
  38. int given = givePresent(rowS, colS, matrix);
  39. presents -= given;
  40. }
  41. break;
  42. case "down":
  43. if (isValid(rowS+1,n)) {
  44. matrix[rowS][colS] = "-";
  45. rowS++;
  46. int given = givePresent(rowS, colS, matrix);
  47. presents -= given;
  48. }
  49. break;
  50. case "left":
  51. if (isValid(colS-1,n)) {
  52. matrix[rowS][colS] = "-";
  53. colS--;
  54. int given = givePresent(rowS, colS, matrix);
  55. presents -= given;
  56. }
  57. break;
  58. case "right":
  59. if (isValid(colS+1,n)) {
  60. matrix[rowS][colS] = "-";
  61. colS++;
  62. int given = givePresent(rowS, colS, matrix);
  63. presents -= given;
  64. }
  65. break;
  66. }
  67. matrix[rowS][colS]="S";
  68. if (presents<=0){
  69. System.out.println("Santa ran out of presents!");
  70. break;
  71. }
  72. command = scanner.nextLine();
  73. }
  74. int disappointedKids = printMatrix(matrix);
  75. if (disappointedKids==0){
  76. System.out.printf("Good job, Santa! %d happy nice kid/s.", goodKids);
  77. }else{
  78. System.out.printf("No presents for %d nice kid/s.",disappointedKids);
  79. }
  80. }
  81.  
  82. private static int printMatrix(String[][] matrix) {
  83. int disappointedKids = 0;
  84. for (int i = 0; i < matrix.length; i++) {
  85. for (int j = 0; j < matrix[i].length; j++) {
  86. if (matrix[i][j].equals("V")){
  87. disappointedKids++;
  88. }
  89. System.out.print(matrix[i][j] + " ");
  90. }
  91. System.out.println();
  92. }
  93. return disappointedKids;
  94. }
  95.  
  96. private static int cookieCase(int rowS, int colS, String[][] matrix) {
  97. int prGiven = 0;
  98. if (isValid(rowS-1,matrix.length)){
  99. prGiven+=givePresent(rowS-1,colS,matrix);
  100. prGiven+=luckyNaughty(rowS-1, colS, matrix);
  101. }
  102. if (isValid(rowS+1, matrix.length)){
  103. prGiven+=givePresent(rowS+1,colS,matrix);
  104. prGiven+=luckyNaughty(rowS+1, colS, matrix);
  105.  
  106. }
  107. if (isValid(colS-1,matrix.length)){
  108. prGiven+=givePresent(rowS, colS-1, matrix);
  109. prGiven+=luckyNaughty(rowS, colS-1, matrix);
  110.  
  111. }
  112. if (isValid(colS+1,matrix.length)){
  113. prGiven+=givePresent(rowS, colS+1,matrix);
  114. prGiven+=luckyNaughty(rowS, colS+1, matrix);
  115.  
  116. }
  117. return prGiven;
  118. }
  119.  
  120. private static int luckyNaughty(int rowS, int colS, String[][] matrix) {
  121. if (matrix[rowS][colS].equals("X")){
  122. matrix[rowS][colS]="-";
  123. return 1;
  124. }
  125. return 0;
  126. }
  127.  
  128. private static int givePresent(int row, int col, String[][]matrix){
  129. if (matrix[row][col].equals("V")){
  130. matrix[row][col]="-";
  131. return 1;
  132. }else if (matrix[row][col].equals("C")){
  133. matrix[row][col]="-";
  134. int n = cookieCase(row, col, matrix);
  135. return 0;
  136. }
  137. return 0;
  138. }
  139. private static boolean isValid(int s, int n) {
  140. return s>=0 && s<n;
  141.  
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement