https://www.acmicpc.net/problem/5212
지도의 크기를 줄여야 하는데 기준을 어떻게 잡을지가 고민이었다.
먼저 남아있는 대륙을 구하고 이 대륙의 행과 열 중 최소, 최대 값을 구한 다음 이를 기준으로 출력.
import java.io.*;
import java.util.*;
public class Main {
int r,c;
int minR = Integer.MAX_VALUE;
int maxR = Integer.MIN_VALUE;
int minC = Integer.MAX_VALUE;
int maxC = Integer.MIN_VALUE;
int[] dx = {-1,1,0,0};
int[] dy = {0,0,-1,1};
char[][] map;
boolean[][] exist;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
void check(int x,int y){
int cnt = 0;
for(int i=0;i<4;i++){
int a = x + dx[i];
int b = y + dy[i];
if(a>=0&&a<r&&b>=0&&b<c){
if(map[a][b]=='X'){
cnt++;
}
}
}
if(cnt>=2){
exist[x][y] = true;
}
}
void solution() throws Exception {
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
map = new char[r][c];
exist = new boolean[r][c];
for(int i=0;i<r;i++){
String s = br.readLine();
for(int j=0;j<c;j++){
map[i][j] = s.charAt(j);
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(map[i][j]=='X'){
check(i,j);
}
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(exist[i][j]){
minR = Math.min(minR,i);
maxR = Math.max(maxR,i);
minC = Math.min(minC,j);
maxC = Math.max(maxC,j);
}
}
}
for(int i=minR;i<=maxR;i++){
for(int j=minC;j<=maxC;j++){
if(exist[i][j]){
System.out.print('X');
}
else{
System.out.print('.');
}
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
new Main().solution();
}
}