TZOJ4521: Flooded Island
描述
There is an beautiful island on the sea, but recently the sea level arround the island is raised, and continuously the land is disrupted. Now, we want to know what it will be like some years later.
The island is describled as a grid of R*C squares, each square has a character:
(1)‘X’: represents the land;
(2)‘.’: represents the sea.
If the land square is currently surrounded by at least 3 sea squares in four direction(north, south, east and west), it will be disrupted.
Now, please output the smallest disrupted map that contains all land squares.
There has at least one square of land will remain in all test cases.
We also assume that the map are surrounded by sea squares.
输入
The input case described the current map of the island.
The first line has two positive integers, R and C (1 <= R, C <= 10), then followed by R lines. Each line has C characters.
输出
Output the rectangluar map of the disrupted island that contains all land squares after some years later.
样例输入
5 3
...
.X.
.X.
.X.
...
样例输出
X
解题思路
本题主要还是题意理解,原题目中的样例给得不好理解,换成下面就好理解了。原题在洛谷。
3 10
…
…XXX.XXX.
XXX…
输出
.XX…X
XX…
这里就是把周围有3个以上.(超出边界也算.)的X变成.,然后找出矩形的最小边界最大边界输出矩形图即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define inf 0x3f3f3f3f
#define IOS ios::sync_with_stdio(0), cin.tie(0)
const ll N = 1e1 * 1 + 5;
ll n, m;
char a[N][N], b[N][N];
ll dir[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void solve() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if(a[i][j] == 'X') {
ll ct = 0;
for (int k = 0; k < 4; ++k) {
ll x = i + dir[k][0];
ll y = j + dir[k][1];
if(a[x][y] != 'X') ++ ct;
}
if(ct < 3) b[i][j] = 'X';
else b[i][j] = '.';
} else {
b[i][j] = a[i][j];
}
}
}
ll u = inf, d = 0, l = inf, r = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if(b[i][j] == 'X') {
u = min(u, i);
d = max(d, i);
l = min(l, j);
r = max(r, j);
}
}
}
for (int i = u; i <= d; ++i) {
for (int j = l; j <= r; ++j) {
cout << b[i][j];
}
cout << endl;
}
}
int main( ){
IOS;
// ll t; cin >> t;
ll t = 1;
while (t--) solve();
return 0;
}