1
unknown
plain_text
2 years ago
3.0 kB
4
Indexable
Never
import java.util.Scanner; public class stones { static int M, N, i, p = 3; static boolean finished; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); int[] stones = new int[N * M]; int R = scanner.nextInt(); String[] splitOne = null; i = 1; scanner.nextLine(); finished = false; String Right[] = new String[R]; for (int i = 0; i < R-1; i++) { Right[i] = scanner.nextLine(); } int j = 0; while (!finished) { splitOne = Right[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++; } } private static void isFinished(int thisNumber, int coordination) { if (thisNumber == 1 && coordination >= N * (M - 1)) { System.out.println(i); finished = true; } } private static void Number(int[] stones, int currentNumber, int previousNumber) { for (int j = 0; j < stones.length; j++) { if (stones[j] == previousNumber) { stones[j] = currentNumber; isFinished(stones[j], j); } } } private static void checkNeighbours(int[] stones, int coordination) { int arrayMin[] = new int[4]; if (stones.length > coordination + 1 && (coordination + 1) % N != 0) { arrayMin[0] = stones[coordination + 1]; } if(coordination -1 > -1) { if (coordination > 0 && (coordination % N != 0)) { arrayMin[1] = stones[coordination - 1]; } } if(stones.length > coordination + N){ arrayMin[2] = stones[coordination + N]; } if(coordination - N > -1){ arrayMin[3] = stones[coordination - N]; } for (int j = 0; j < arrayMin.length; j++) { if(arrayMin[j] < stones[coordination] && arrayMin[j] != 0){ Number(stones, arrayMin[j], stones[coordination]); } } for (int j = 0; j < arrayMin.length; j++) { if(arrayMin[j] > stones[coordination]){ Number(stones, stones[coordination], arrayMin[j]); } } } private static void insert(int[] stones, int Ni, int Mi) { int coordination = Mi * N + Ni; if (coordination < N) { stones[coordination] = 1; checkNeighbours(stones, coordination); }else{ stones[coordination] = p++; checkNeighbours(stones, coordination); } } }