版本比较
题解:B4474 [厦门小学生 C++ 2025] 故障机器人
正文
增加 2 行删除 88 行
8 行未改动910111213131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101158 行未改动有了这个思路,我们就可以从实验室开始 `bfs`,如果这个格子一定能到实验室,就把它加入队列里。
### 注意事项
这题的数据范围($n,m \le 10^6,nm \le 10^6$)比较 ex,所以为了方便用 `string` 数字存地图,然后不需要打标记,直接在原图标记 `+` 即可。这题的数据范围($n,m \le 10^6,nm \le 10^6$)比较 ex,所以为了方便用 `string` 数组存地图,然后不需要打标记,直接在原图标记 `+` 即可。
## AC Code
```cpp#include<cmath>#include<queue>#include<cstring>#include<iostream>#include<algorithm>using namespace std;
#ifdef ONLINE_JUDGE#define getchar getchar_unlocked#define putchar putchar_unlocked#endiftemplate<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;}```## [AC Code](https://www.luogu.com.cn/paste/ofrujkzu)