-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrange Island.txt
94 lines (80 loc) · 3.35 KB
/
Strange Island.txt
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
87
88
89
90
91
92
93
94
Strange island
Send Feedback
A popular tourist destination country is situated on a breathtakingly beautiful archipelago constantly bathed by the sun. The country's residents are very proud of their numerous islands. However, global warming has them very worried: raising sea levels are resulting in rapidly increasing loss of dry land, which is diminishing the beauty of the archipelago.
The map of the archipelago is represented by a grid of R by C squares (characters). The character 'X' (uppercase letter x) represents dry land, while '.' (period) represents sea. It has been estimated that, in fifty years, sea will have flooded every square of land that is currently surrounded by sea on three or on all four sides (north, south, east, west). Assume that all squares outside the map (along the edges) are covered by sea.
Your task is computing the map of the archipelago in fifty years (after the described sea level rise).
Since there will probably be less land than today, you shouldn't print out the whole map, but only its smallest rectangular part that contains all land squares. It is guaranteed that at least one square of land will remain in all test cases.
Input Format:
The first line of input contains two positive integers, R and C, the dimensions of the current map.
Each of the following R lines contains C characters. These R by C characters represent the current map of the archipelago.
Constraints:
1 ≤ R, C ≤ 10
Time limit: 1 sec
Output Format:
The output must contain an appropriate number of lines representing the required rectangular part of the future (flooded) map.
Sample Input 1:
5 3
...
.X.
.X.
.X.
...
Sample Output 1:
X
Sample Input 2:
3 10
..........
..XXX.XXX.
XXX.......
Sample Output 2:
.XX...X
XX.....
///////////////////////=====================================>>>>>>>>>>>>>>>>>>>>>>>>>>
import java.util.Scanner;
public class Main {
public static String[] map;
public static char [][] newMap = new char[12][12];
public static int[] u = {0, 0, 1, -1};
public static int[] v = {1, -1, 0, 0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int s = sc.nextInt();
map = new String[r];
int mini = 100;
int maxi = 0;
int minj = 100;
int maxj = 0;
for(int i = 0; i < r; i++) {
map[i] = sc.next();
}
for(int i = 0; i< r; i++) {
for(int j = 0; j < s; j++) {
int more = 0;
for(int k = 0; k < 4; k++) {
int x = i + u[k];
int y = j + v[k];
if(x < 0 || y < 0 || x >= r || y >= s || map[x].charAt(y) == '.') {
more++;
}
}
if(more >= 3)
newMap[i][j] = '.';
else
newMap[i][j] = map[i].charAt(j);
if(newMap[i][j] == 'X') {
mini = Math.min(mini, i);
maxi = Math.max(maxi, i);
minj = Math.min(minj, j);
maxj = Math.max(maxj, j);
}
}
}
for(int i = mini; i <= maxi; i++) {
for(int j = minj; j <= maxj; j++) {
System.out.print(newMap[i][j]);
}
System.out.println();
}
}
}