9358. ARHIPELAG
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
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
The first line of input contains two positive integers, R and C (1 ≤ R, C ≤ 10), 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.
Output
The output must contain an appropriate number of lines representing the required rectangular part of
the future (flooded) map.
Sample Input
Sample input1 5 3 ... .X. .X. .X. ... Sample input2 3 10 .......... ..XXX.XXX. XXX.......
Sample Output
Sample output1 X Sample output2 .XX...X XX.....
模拟水题咯
#include <stdio.h>
char mapp[11][11], new_map[11][11];
int h, w, min_h, min_w, max_h, max_w;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
bool is_sea(int i, int j) {
if (i < 0 || j < 0 || i >= h || j >= w || mapp[i][j] == '.')
return true;
return false;
}
bool is_all_sea(bool is_col, int sp, int ep, int num) {
if (is_col) {
for (int i = sp; i < ep; i++) {
if (new_map[i][num] == 'X') {
return false;
}
}
return true;
} else {
for (int i = sp; i < ep; i++) {
if (new_map[num][i] == 'X') {
return false;
}
}
return true;
}
}
void minimize_map() {
for (int i = min_h; i < max_h; i++) {
if (is_all_sea(0, min_w, max_w, i)) {
min_h++;
} else {
break;
}
}
for (int i = max_h - 1; i >= min_h; i--) {
if (is_all_sea(0, min_w, max_w, i)) {
max_h--;
} else {
break;
}
}
for (int i = min_w; i < max_w; i++) {
if (is_all_sea(1, min_h, max_h, i)) {
min_w++;
} else {
break;
}
}
for (int i = max_w - 1; i >= min_w; i--) {
if (is_all_sea(1, min_h, max_h, i)) {
max_w--;
} else {
break;
}
}
}
int main() {
scanf("%d %d\n", &h, &w);
for (int i = 0; i < h; i++) {
scanf("%s", mapp[i]);
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (mapp[i][j] == 'X') {
int counter = 0;
for (int k = 0; k < 4; k++) {
if (is_sea(i + dir[k][0], j + dir[k][1])) {
counter++;
}
}
if (counter >= 3) {
new_map[i][j] = '.';
} else {
new_map[i][j] = 'X';
}
} else {
new_map[i][j] = '.';
}
}
}
max_h = h;
max_w = w;
min_h = 0;
min_w = 0;
minimize_map();
for (int i = min_h; i < max_h; i++) {
for (int j = min_w; j < max_w; j++) {
printf("%c", new_map[i][j]);
}
printf("\n");
}
return 0;
}