2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는

www.acmicpc.net

 

 

 


 

 

  • 풀이

 

물이 잠기는 높이는 1이상 100이하로 제한되어 있습니다.

 

1~100까지의 높이만 보면되는데 배열안에 가장 작은 값 min, 가장 큰 값 max를 가져와서 min~max의 값까지만 확인해주었습니다. (필요없는 작업 X)

 

min~max의 높이 순서대로 배열을 돌리면서 안전하면서 check되지 않은 영역을 찾게되면 연결된 모든 안전한 영역을 bfs방식으로 check시켜 줍니다. (같은 안전 영역을 다시 세지 않기 위함) 그 후 answer의 값보다 안전한 영역이 많은 경우 answer을 해당 값으로 초기화 시켜줍니다.

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {
	static int answer, N, min, max;
	static int[][] array;
	static boolean[][] check;
	static int[] x = {0,0,-1,1};
	static int[] y = {-1,1,0,0};
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SetData();
		System.out.println(answer);
	}
	
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);
		
		N = in.nextInt();
		array = new int[N][N];
		answer = 1;
		min = Integer.MAX_VALUE;
		max = Integer.MIN_VALUE;

		for(int i = 0 ; i < N; i++) {
			for(int j = 0; j < N; j++) {
				array[i][j] = in.nextInt();
				min = Math.min(min, array[i][j]);
				max = Math.max(max, array[i][j]);
			}
		}
        
		
		for(int i = min; i <= max; i++) {
			int count = 0;
			
			check = new boolean[N][N];
			
			for(int a = 0; a < N; a++) {
				for(int b = 0; b < N; b++) {
					if(array[a][b] <= i || check[a][b]) continue;
						
					bfs(a,b,i);
					count++;
				}
			}
			
			answer = Math.max(answer, count);
		}
	}
	
	private static void bfs(int a, int b, int height) {
		check[a][b] = true;
		
		for(int direction = 0; direction < 4; direction++) {
			int r = a + x[direction];
			int c = b + y[direction];
			
			if(r < 0 || r >= N || c < 0 || c >= N) continue;
			if(check[r][c] || array[r][c] <= height) continue;
			
			bfs(r,c, height);
		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts