|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + |
| 6 | + private static int[][] virus; |
| 7 | + private static int N; |
| 8 | + private static PriorityQueue<int[]> q; |
| 9 | + private static boolean[][] visited; |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 14 | + N = Integer.parseInt(st.nextToken()); |
| 15 | + int K = Integer.parseInt(st.nextToken()); |
| 16 | + virus = new int[N][N]; |
| 17 | + q = new PriorityQueue<>((o1, o2) -> Integer.compare(virus[o1[0]][o1[1]], virus[o2[0]][o2[1]])); |
| 18 | + visited = new boolean[N][N]; |
| 19 | + for(int i=0; i<N; i++) { |
| 20 | + st = new StringTokenizer(br.readLine()); |
| 21 | + for(int j=0; j<N; j++) { |
| 22 | + virus[i][j] = Integer.parseInt(st.nextToken()); |
| 23 | + } |
| 24 | + } |
| 25 | + st = new StringTokenizer(br.readLine()); |
| 26 | + int S = Integer.parseInt(st.nextToken()); |
| 27 | + int X = Integer.parseInt(st.nextToken()); |
| 28 | + int Y = Integer.parseInt(st.nextToken()); |
| 29 | + for(int i=0; i<N; i++) { |
| 30 | + for(int j=0; j<N; j++) { |
| 31 | + if(virus[i][j] > 0 && !visited[i][j]) { |
| 32 | + visited[i][j] = true; |
| 33 | + q.offer(new int[]{i, j}); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + for(int i=0; i<S; i++) { |
| 38 | + bfs(); |
| 39 | + } |
| 40 | + System.out.println(virus[X-1][Y-1]); |
| 41 | + } |
| 42 | + |
| 43 | + public static void bfs() { |
| 44 | + int[] dh = {0, 1, 0, -1}; |
| 45 | + int[] dw = {-1, 0, 1, 0}; |
| 46 | + List<int[]> nextVirus = new ArrayList<>(); |
| 47 | + while(!q.isEmpty()) { |
| 48 | + int h = q.peek()[0]; |
| 49 | + int w = q.peek()[1]; |
| 50 | + q.poll(); |
| 51 | + for(int i=0; i<4; i++) { |
| 52 | + int nextH = h + dh[i]; |
| 53 | + int nextW = w + dw[i]; |
| 54 | + if(nextH >= 0 && nextH < N && nextW >= 0 && nextW < N) { |
| 55 | + if(!visited[nextH][nextW]) { |
| 56 | + visited[nextH][nextW] = true; |
| 57 | + virus[nextH][nextW] = virus[h][w]; |
| 58 | + nextVirus.add(new int[]{nextH, nextW}); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + for(int i=0; i<nextVirus.size(); i++) { |
| 64 | + q.offer(nextVirus.get(i)); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments