bah bah baaaah

husk kald den nye klasse du laver "main"
 avatar
unknown
plain_text
3 years ago
3.4 kB
5
Indexable
import java.util.Scanner;

public class main {
    static int N, M, i, plus = 3; //To keep it simpler
    static boolean done;

    private static void isDone(int thisNumber, int coord) {
        if (thisNumber == 1 && coord >= N * (M - 1)) {
            System.out.println(i);
            done = true;
            System.exit(0);
        }
    }

    private static void changeTheNumbers(int[] stones, int newNumber, int oldNumber) {
        for (int j = 0; j < stones.length; j++) {
            if (stones[j] == oldNumber) {
                stones[j] = newNumber;
                isDone(stones[j], j);
            }
        }
    }

    private static void checkIfNeighboursNearbyAreLessOrChangeThem(int[] stones, int coord) {

        int arrayForLowest[] = new int[4];


        if (stones.length > coord + 1 && (coord + 1) % N != 0) { //if there is a right side neighbour and this coord is not at last on the table
            arrayForLowest[0] = stones[coord + 1];
        }

        if(coord-1 > -1) {
            if (coord > 0 && (coord % N != 0)) { //if there is a left coord and it is not the first on the table
                arrayForLowest[1] = stones[coord - 1];
            }
        }

        if(stones.length > coord + N){ //If there is an under-neighbour
            arrayForLowest[2] = stones[coord + N];
        }

        if(coord - N > -1){ //If there is an upper neighbour
            arrayForLowest[3] = stones[coord - N];
        }

        for (int j = 0; j < arrayForLowest.length; j++) {
            if(arrayForLowest[j] < stones[coord] && arrayForLowest[j] != 0){
                changeTheNumbers(stones, arrayForLowest[j], stones[coord]);
            }
        }

        for (int j = 0; j < arrayForLowest.length; j++) {
            if(arrayForLowest[j] > stones[coord]){
                changeTheNumbers(stones, stones[coord], arrayForLowest[j]);
            }
        }
    }


    private static void insert(int[] stones, int ni, int mi) {

        int coord = mi * N + ni; //These are the coordinates of where to place a number in the array

        if (coord < N) {
            stones[coord] = 1;
            checkIfNeighboursNearbyAreLessOrChangeThem(stones, coord);
        }else{
            stones[coord] = plus++;
            checkIfNeighboursNearbyAreLessOrChangeThem(stones, coord);
        }
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        N = scanner.nextInt();
        M = scanner.nextInt();

        int[] stones = new int[N * M]; //Create array of the length of N*M table

        int R = scanner.nextInt();

        String[] splitOne = null;
        scanner.nextLine(); //To fix a bug

        i = 1; //This will eventually be printed at last (rounds of removing stones)
        done = false; //To keep in while loop

        String Rline[] = new String[R];

        for (int i = 0; i < R-1; i++) {
            Rline[i] = scanner.nextLine();
        }

        int j = 0;

        while (!done) {
            splitOne = Rline[j].split(" ");

            int[] splitFinal = new int[2];

            splitFinal[0] = Integer.parseInt(splitOne[0]);
            splitFinal[1] = Integer.parseInt(splitOne[1]);

            insert(stones, splitFinal[0], splitFinal[1]);
            j++;
            i++;
        }
    }
}

Editor is loading...