题目简述
有一个永远不按你指令走的机器人,求地图中哪些格子不管机器人怎么走都能到达实验室。
主要思路
要让机器人怎么走都能到实验室,就是让机器人除了指令的那一格其他能走的格子也都能到实验室,我们可以这样判断:计算机器人周围不能到实验室的格子的数量,如果这个数小于等于 ,那么就能到实验室(为 代表每个格子都能到实验室当然可以,为 把指令设为不能到实验室的那个格子即可)。
有了这个思路,我们就可以从实验室开始 bfs,如果这个格子一定能到实验室,就把它加入队列里。
注意事项
这题的数据范围()比较 ex,所以为了方便用 string 数字存地图,然后不需要打标记,直接在原图标记 + 即可。
AC Code
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template<typename T> void read(T& x) { int f = 1; x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-')f = -1; ch = getchar(); }while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }x *= f; }
template<typename T, typename ...Args> void read(T& x, Args &...args) { read(x); read(args...); }
template<typename T> void print(T x) { if (x < 0) { putchar('-'); x = -x; }if (x > 9) { print(x / 10); }putchar(char(x % 10 + 48)); }
template<typename T, typename ...Args> void print(T x, Args ...args) { print(x); putchar(' '); print(args...); }
void readstr(string& x) { x = ""; char ch = getchar(); while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t') ch = getchar(); while (ch != ' ' && ch != '\n' && ch != '\r' && ch != '\t') x += ch, ch = getchar(); }
template<typename ...Args> void readstr(string& x, Args &...args) { readstr(x); readstr(args...); }
void printstr(string x) { for (char i : x) putchar(i); }
template<typename ...Args> void printstr(string x, Args ...args) { printstr(x); putchar(' '); printstr(args...); }
const int N = 1e6 + 10;
const int INT_INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
template<typename T1, typename T2> T1 _pow(T1 a, T2 b) { T1 x = 1, y = a; while (b > 0) { if (b & 1) x *= y; y *= y; b >>= 1; } return x; }
template<typename T1, typename T2, typename T3> T1 _pow(T1 a, T2 b, T3 mod) { T1 x = 1, y = a; while (b > 0) { if (b & 1) x = x * y % mod; y = y * y % mod; b >>= 1; } return x; }
// ----------------------------
struct node {
int x, y;
};
// ----------------------------
string plat[N];
int dx[4] = { -1, 0, 0, 1 }, dy[4] = { 0, -1, 1, 0 };
// ----------------------------
bool check(int x, int y, int n, int m) {
if (x <= 0 || x > n || y <= 0 || y > m || plat[x][y] != '.') return false;
int cnt = 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx > 0 && nx <= n && ny > 0 && ny <= m && plat[nx][ny] == '.') cnt++;
}
return (cnt <= 1);
}
void bfs(int n, int m) {
int posx, posy;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (plat[i][j] == 'L') posx = i, posy = j;
}
}
queue<node> q;
q.push({ posx, posy });
while (!q.empty()) {
node u = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = u.x + dx[i], ny = u.y + dy[i];
if (check(nx, ny, n, m)) {
plat[nx][ny] = '+';
q.push({ nx, ny });
}
}
}
}
int main() {
int t; read(t);
// ----------------------------
while (t--) {
int n, m; read(n, m);
for (int i = 1; i <= n; i++) {
readstr(plat[i]);
plat[i] = ' ' + plat[i];
}
bfs(n, m);
for (int i = 1; i <= n; i++) {
printstr(plat[i].substr(1));
putchar('\n');
}
}
// ----------------------------
return 0;
}