链接:https://ac.nowcoder.com/acm/contest/11121/C
来源:牛客网
题目描述
经过多年的征战,牛牛在与牛可乐的对决渐渐处于下风,于是牛牛决定对牛可乐来一次大围剿。
战场可以看作一张 n*mn∗m 的地图,牛可乐的士兵只能上下左右移动,不能斜着移动,牛牛决定挖一圈陷阱包围牛可乐的士兵。牛牛想知道包围牛可乐的士兵所需要的最少的陷阱数量是多少(划掉,具体请看update),但是牛牛并不会排兵布阵,于是只能求助于你了。
保证地图的边界处不会有士兵.
保证牛可乐的士兵是连通的
要求牛可乐使用的陷阱构成的包围圈与牛可乐的士兵之间要求是紧密接触的
输入描述:
第一行输入两个整数 nn 和 mm,表示地图的大小
下面 nn 行每行 mm 个字符,… 表示空地,## 表示士兵。
保证输出的字符串只包含 … 和 ##
输出描述:
输出挖完陷阱后的地图,陷阱用 *∗ 来表示.
示例1
输入
复制
6 5
…
.###.
.#.#.
.###.
…##.
…
输出
复制
..
###
#.#
###
.##
….
示例2
输入
复制
10 10
…
…######…
.#######…
.#######…
.##.###…
.##…##…
…##…
…
…
…
输出
复制
……
.######.
#######.
#######.
#####…
*####…
.**.##*…
…**…
…
…
说明
备注:
1\leq n,m\leq5001≤n,m≤500
发现一个点可以变成点的话,说明这个点的四个方向有一个是士兵并且这个点可以走出去
判断一个点可以走出去的话,可以从四个边bfs即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std;
#define pair(a, b) make_pair(a, b)
#define memset(a, b) memset(a, b, sizeof a)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define x first
#define y second
typedef __int128 INT;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 510;
const int M = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int P = 13331;
char s[N][N];
int n, m;
bool st[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool f[N][N];
int main(){
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++){
scanf("%s", s[i] + 1);
}
queue<PII> q;
for (int i = 1; i <= n; i ++){
f[i][1] = true;
f[i][m] = true;
st[i][1] = true;
st[i][m] = true;
q.push({i, 1});
q.push({i, m});
}
for (int i = 1; i <= m; i ++){
f[1][i] = true;
f[n][i] = true;
st[1][i] = true;
st[n][i] = true;
q.push({1, i});
q.push({n, i});
}
while(q.size()){
auto t = q.front();
q.pop();
int xx = t.x, yy = t.y;
//if (st[xx][yy]) continue;
// st[xx][yy] = true;
for (int i = 0; i < 4; i ++){
int xxx = xx + dx[i], yyy = yy + dy[i];
if (s[xxx][yyy] == '#') continue;
if (xxx < 1 || xxx > n || yyy < 1 || yyy > m) continue;
if (st[xxx][yyy]) continue;
st[xxx][yyy] = true;
f[xxx][yyy] = true;
//cout << xxx << "-------" << yyy << endl;
q.push({xxx, yyy});
}
}
// if (f[5][2]) cout << "---------" << endl;
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= m; j ++){
if (s[i][j] == '#') continue;
// memset(st, 0);
bool flag1 = false;
bool flag2 = false;
for (int k = 0; k < 4; k ++){
int x = i + dx[k], y = j + dy[k];
if (s[x][y] == '#') flag1 = true;
}
if (!flag1) continue;
if (f[i][j]) flag2 = true;
// if (i == 1 || i == n || j == 1|| j == m) continue;
if (flag1 && flag2) s[i][j] = '*';
}
}
for (int i = 1; i <= n; i ++){
printf("%s\n", s[i] + 1);
}
return 0;
}