1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
| import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
static String[][]input;
static int[]dr= {0, -1, 0, 1};
static int[]dc= {-1, 0, 1, 0};
static intR,C;
static String[][]map;
static LinkedList<Node>list= new LinkedList<>();
public static void main(String[] ars) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
R= Integer.parseInt(st.nextToken());
C= Integer.parseInt(st.nextToken());
map= new String[R][C];
for(int i = 0; i<R; i++){
String[] input = br.readLine().split("");
for(int j = 0; j<C; j++){
map[i][j] = input[j];
}
}
for(int i =0 ;i<R; i++){
for(int j = 0; j<C; j++){
if(map[i][j].equals("X")){
bfs(i,j);
}
}
}
for(int i = 0; i<list.size(); i++){
int a =list.get(i).r;
int b =list.get(i).c;
map[a][b] = ".";
}
int x = Integer.MAX_VALUE;
int y = Integer.MAX_VALUE;
int a = 0;
int b = 0;
for (int i = 0; i <R; i++) {
for (int j = 0; j <C; j++) {
if(map[i][j].equals("X")){
x = Math.min(x,i);
y = Math.min(y,j);
a = Math.max(a,i);
b = Math.max(b,j);
}
}
}
for(int i = x; i<=a; i++){
for(int j = y; j<=b; j++){
System.out.print(map[i][j]);
}
System.out.println();
}
}
static void bfs(int r, int c){
int landNum = 0;
for(int i = 0; i<4; i++) {
int nr = r +dr[i];
int nc = c +dc[i];
if(nr < 0 || nr >=R|| nc < 0 || nc >=C) {
continue;
}
if (map[nr][nc].equals("X")) landNum++;
}
if (landNum < 2) {
list.add(new Node(r,c));
}
}
}
class Node{
int r;
int c;
public Node(int r, int c) {
this.r = r;
this.c = c;
}
}
|