Advertisement
Guest User

Untitled

a guest
Jun 26th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static int santaRow;
  5. public static int santaCow;
  6. public static int niceKid = 0;
  7. public static int presentsCount=0;
  8.  
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11.  
  12. int presents = Integer.parseInt(sc.nextLine());
  13. presentsCount=presents;
  14. int sizeM = Integer.parseInt(sc.nextLine());
  15.  
  16. char[][] matrix = new char[sizeM][sizeM];
  17.  
  18. readMatrix(matrix, sc);
  19.  
  20. String inside;
  21. while (!(inside = sc.nextLine()).equals("Christmas morning") ) {
  22. switch (inside) {
  23. case "up":
  24. santaRow--;
  25. if(checkIndex(matrix,santaRow,santaCow)) {
  26. if(matrix[santaRow][santaCow]=='C'){
  27. checkVorXorEmpty(matrix,santaRow+1,santaCow);
  28. checkVorXorEmpty(matrix,santaRow-1,santaCow);
  29. checkVorXorEmpty(matrix,santaRow,santaCow+1);
  30. checkVorXorEmpty(matrix,santaRow,santaCow-1);
  31. }else {
  32. checkVorXorEmpty(matrix, santaRow, santaCow);
  33. }
  34. }else{
  35. System.out.println("Santa ran out of presents!");
  36. }
  37. break;
  38. case "down":
  39. santaRow++;
  40. if(checkIndex(matrix,santaRow,santaCow)) {
  41. if(matrix[santaRow][santaCow]=='C'){
  42. checkVorXorEmpty(matrix,santaRow+1,santaCow);
  43. checkVorXorEmpty(matrix,santaRow-1,santaCow);
  44. checkVorXorEmpty(matrix,santaRow,santaCow+1);
  45. checkVorXorEmpty(matrix,santaRow,santaCow-1);
  46. }else {
  47. checkVorXorEmpty(matrix, santaRow, santaCow);
  48. }
  49. }else{
  50. System.out.println("Santa ran out of presents!");
  51. }
  52. break;
  53. case "right":
  54. santaCow++;
  55. if(checkIndex(matrix,santaRow,santaCow)) {
  56. if(matrix[santaRow][santaCow]=='C'){
  57. checkVorXorEmpty(matrix,santaRow+1,santaCow);
  58. checkVorXorEmpty(matrix,santaRow-1,santaCow);
  59. checkVorXorEmpty(matrix,santaRow,santaCow+1);
  60. checkVorXorEmpty(matrix,santaRow,santaCow-1);
  61. }else {
  62. checkVorXorEmpty(matrix, santaRow, santaCow);
  63. }
  64. }else{
  65. System.out.println("Santa ran out of presents!");
  66. }
  67. break;
  68. case "left":
  69. santaCow--;
  70. if(checkIndex(matrix,santaRow,santaCow)) {
  71. if(matrix[santaRow][santaCow]=='C'){
  72. checkVorXorEmpty(matrix,santaRow+1,santaCow);
  73. checkVorXorEmpty(matrix,santaRow-1,santaCow);
  74. checkVorXorEmpty(matrix,santaRow,santaCow+1);
  75. checkVorXorEmpty(matrix,santaRow,santaCow-1);
  76. }else {
  77. checkVorXorEmpty(matrix, santaRow, santaCow);
  78. }
  79. }else{
  80. System.out.println("Santa ran out of presents!");
  81. }
  82. break;
  83. }
  84. }
  85.  
  86. matrix[santaRow][santaCow] = 'S';
  87. printMatrix(matrix);
  88. //if(presentsCount<=niceKid){
  89. System.out.printf("No presents for %d nice kid/s.%n",(niceKid-presentsCount));
  90. // }else{
  91. // System.out.printf("Good job, Santa! %d happy nice kid/s.%n",niceKid);
  92. //}
  93. }
  94.  
  95. private static void checkVorXorEmpty(char[][] matrix, int santaRow, int santaCow) {
  96. if(matrix[santaRow][santaCow]=='X'){
  97. matrix[santaRow][santaCow]='-';
  98. }
  99. if(matrix[santaRow][santaCow]=='V'){
  100. matrix[santaRow][santaCow]='-';
  101. presentsCount-=1;
  102. }
  103. if(matrix[santaRow][santaCow]=='-'){
  104. matrix[santaRow][santaCow]='-';
  105. }
  106. }
  107.  
  108. private static void readMatrix(char[][] matrix, Scanner scanner) {
  109. for (int r = 0; r < matrix.length; r++) {
  110. String input = scanner.nextLine();
  111. input.replace(" ", "");
  112. char[] intake = input.toCharArray();
  113. matrix[r] = intake;
  114. for (int c = 0; c < matrix[r].length; c++) {
  115. matrix[r][c] = intake[c];
  116. if (matrix[r][c] == 'S') {
  117. santaRow = r;
  118. santaCow = c;
  119. matrix[santaRow][santaCow] = '-';//TODO check
  120. }
  121. if(matrix[r][c]=='V'){
  122. matrix[r][c]='-';
  123. niceKid++;
  124. }
  125. }
  126. }
  127. }
  128.  
  129. private static void printMatrix(char[][] matrix) {
  130. for (int Rp = 0; Rp < matrix.length; Rp++) {
  131. for (int Cp = 0; Cp < matrix[Rp].length; Cp++) {
  132. System.out.print(matrix[Rp][Cp]);
  133. }
  134. System.out.println();
  135. }
  136. }
  137. private static boolean checkIndex(char[][]matriks,int santaRow2,int santaCow2){
  138. return santaRow2>=0 && santaRow2<matriks.length && santaCow2>=0 && santaCow2<matriks[santaRow2].length;
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement