Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static int santaRow;
- public static int santaCow;
- public static int niceKid = 0;
- public static int presentsCount=0;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int presents = Integer.parseInt(sc.nextLine());
- presentsCount=presents;
- int sizeM = Integer.parseInt(sc.nextLine());
- char[][] matrix = new char[sizeM][sizeM];
- readMatrix(matrix, sc);
- String inside;
- while (!(inside = sc.nextLine()).equals("Christmas morning") ) {
- switch (inside) {
- case "up":
- santaRow--;
- if(checkIndex(matrix,santaRow,santaCow)) {
- if(matrix[santaRow][santaCow]=='C'){
- checkVorXorEmpty(matrix,santaRow+1,santaCow);
- checkVorXorEmpty(matrix,santaRow-1,santaCow);
- checkVorXorEmpty(matrix,santaRow,santaCow+1);
- checkVorXorEmpty(matrix,santaRow,santaCow-1);
- }else {
- checkVorXorEmpty(matrix, santaRow, santaCow);
- }
- }else{
- System.out.println("Santa ran out of presents!");
- }
- break;
- case "down":
- santaRow++;
- if(checkIndex(matrix,santaRow,santaCow)) {
- if(matrix[santaRow][santaCow]=='C'){
- checkVorXorEmpty(matrix,santaRow+1,santaCow);
- checkVorXorEmpty(matrix,santaRow-1,santaCow);
- checkVorXorEmpty(matrix,santaRow,santaCow+1);
- checkVorXorEmpty(matrix,santaRow,santaCow-1);
- }else {
- checkVorXorEmpty(matrix, santaRow, santaCow);
- }
- }else{
- System.out.println("Santa ran out of presents!");
- }
- break;
- case "right":
- santaCow++;
- if(checkIndex(matrix,santaRow,santaCow)) {
- if(matrix[santaRow][santaCow]=='C'){
- checkVorXorEmpty(matrix,santaRow+1,santaCow);
- checkVorXorEmpty(matrix,santaRow-1,santaCow);
- checkVorXorEmpty(matrix,santaRow,santaCow+1);
- checkVorXorEmpty(matrix,santaRow,santaCow-1);
- }else {
- checkVorXorEmpty(matrix, santaRow, santaCow);
- }
- }else{
- System.out.println("Santa ran out of presents!");
- }
- break;
- case "left":
- santaCow--;
- if(checkIndex(matrix,santaRow,santaCow)) {
- if(matrix[santaRow][santaCow]=='C'){
- checkVorXorEmpty(matrix,santaRow+1,santaCow);
- checkVorXorEmpty(matrix,santaRow-1,santaCow);
- checkVorXorEmpty(matrix,santaRow,santaCow+1);
- checkVorXorEmpty(matrix,santaRow,santaCow-1);
- }else {
- checkVorXorEmpty(matrix, santaRow, santaCow);
- }
- }else{
- System.out.println("Santa ran out of presents!");
- }
- break;
- }
- }
- matrix[santaRow][santaCow] = 'S';
- printMatrix(matrix);
- //if(presentsCount<=niceKid){
- System.out.printf("No presents for %d nice kid/s.%n",(niceKid-presentsCount));
- // }else{
- // System.out.printf("Good job, Santa! %d happy nice kid/s.%n",niceKid);
- //}
- }
- private static void checkVorXorEmpty(char[][] matrix, int santaRow, int santaCow) {
- if(matrix[santaRow][santaCow]=='X'){
- matrix[santaRow][santaCow]='-';
- }
- if(matrix[santaRow][santaCow]=='V'){
- matrix[santaRow][santaCow]='-';
- presentsCount-=1;
- }
- if(matrix[santaRow][santaCow]=='-'){
- matrix[santaRow][santaCow]='-';
- }
- }
- private static void readMatrix(char[][] matrix, Scanner scanner) {
- for (int r = 0; r < matrix.length; r++) {
- String input = scanner.nextLine();
- input.replace(" ", "");
- char[] intake = input.toCharArray();
- matrix[r] = intake;
- for (int c = 0; c < matrix[r].length; c++) {
- matrix[r][c] = intake[c];
- if (matrix[r][c] == 'S') {
- santaRow = r;
- santaCow = c;
- matrix[santaRow][santaCow] = '-';//TODO check
- }
- if(matrix[r][c]=='V'){
- matrix[r][c]='-';
- niceKid++;
- }
- }
- }
- }
- private static void printMatrix(char[][] matrix) {
- for (int Rp = 0; Rp < matrix.length; Rp++) {
- for (int Cp = 0; Cp < matrix[Rp].length; Cp++) {
- System.out.print(matrix[Rp][Cp]);
- }
- System.out.println();
- }
- }
- private static boolean checkIndex(char[][]matriks,int santaRow2,int santaCow2){
- return santaRow2>=0 && santaRow2<matriks.length && santaCow2>=0 && santaCow2<matriks[santaRow2].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement