kyrie_lrving1992 @ 2023-05-01 14:48:07
#include <bits/stdc++.h>
using namespace std;
int _n = 0;
char _map[1500][1500];
bool _p[1500][1500];
bool _vis[1500][1500];
int _head , _tail;
int _cnt = 0;
int _queue[15000000][2];
int _EX[4] = {0 , 0 , 1 , -1};
int _EY[4] = {1 , -1 , 0 , 0};
int _ans = 0;
int _head1 , _tail1;
int _queue1[15000000][2];
int _vis1[1500][1500];
void BFS(int x , int y){
_head = 1 , _tail = 1;
_queue[_head][0] = x;
_queue[_head][1] = y;
while(_head <= _tail){
int CurX , CurY;
for(int i = 0 ; i <= 3 ; i++){
CurX = _queue[_head][0] + _EX[i];
CurY = _queue[_head][1] + _EY[i];
if(CurX >= 1 && CurX <= _n && CurY >= 1 && CurY <= _n && _map[CurX][CurY] == '#' && _vis[CurX][CurY] == false){
_tail++;
_vis[CurX][CurY] = true;
_queue[_tail][0] = CurX;
_queue[_tail][1] = CurY;
}
}
_head++;
}
}
void BFS1(int x , int y){
_head1 = 1 , _tail1 = 1;
_queue1[_head1][0] = x;
_queue1[_head1][1] = y;
while(_head1 <= _tail1){
int CurX , CurY;
for(int i = 0 ; i <= 3 ; i++){
CurX = _queue1[_head1][0] + _EX[i];
CurY = _queue1[_head1][1] + _EY[i];
if(CurX >= 1 && CurX <= _n && CurY >= 1 && CurY <= _n && _map[CurX][CurY] == '#' && _vis1[CurX][CurY] == false){
_tail1++;
_vis1[CurX][CurY] = true;
_queue1[_tail1][0] = CurX;
_queue1[_tail1][1] = CurY;
}
}
_head1++;
}
}
int main(){
cin >> _n;
for(int i = 1 ; i <= _n ; i++){
for(int j = 1 ; j <= _n ; j++){
cin >> _map[i][j];
}
}
for(int i = 1 ; i <= _n ; i++){
for(int j = 1 ; j <= _n ; j++){
if(_vis1[i][j] == false && _map[i][j] == '#'){
_cnt++;
BFS1(i , j);
}
}
}
for(int i = 1 ; i <= _n ; i++){
for(int j = 1 ; j <= _n ; j++){
if(_map[i][j] == '#' && _vis[i][j] == false){
if(_map[i][j - 1] == '#' && _map[i][j + 1] == '#' && _map[i - 1][j] == '#' && _map[i + 1][j] == '#'){
_p[i][j] = true;
}
}
}
}
for(int i = 1 ; i <= _n ; i++){
for(int j = 1 ; j <= _n ; j++){
if(_p[i][j] == true){
_map[i][j] = '#';
}
else{
_map[i][j] = '.';
}
}
}
for(int i = 1 ; i <= _n ; i++){
for(int j = 1 ; j <= _n ; j++){
if(_vis[i][j] == false && _map[i][j] == '#'){
_ans++;
BFS(i , j);
}
}
}
cout << _cnt - _ans;
return 0;
}
by hjqhs @ 2023-05-01 14:53:17
首先学习一下STL的队列
其次学习一下四联通问题
by kyrie_lrving1992 @ 2023-05-01 14:54:16
@hjqhs 啊这,没有更快速的方法了么(doge
by hjqhs @ 2023-05-01 14:55:38
因为你的bfs不用STL的队列而是手写,让很多人都不想看
by kyrie_lrving1992 @ 2023-05-01 14:56:17
@hjqhs 额,队列到时学过,但是懒得改了(bushi
by kyrie_lrving1992 @ 2023-05-01 14:56:51
我觉得手写也还可以
by hjqhs @ 2023-05-01 14:58:08
手写比STL快一点点 但没这个必要
by kyrie_lrving1992 @ 2023-05-01 14:58:35
好吧
by hjqhs @ 2023-05-01 14:59:31
https://www.luogu.com.cn/discuss/597095
by hjqhs @ 2023-05-01 15:00:33
注意:被海水淹没后的陆地应用另一个字符表示,而不是把它变为海洋,这个点可以便利,但不能被当作起点,不然就只有 36 分。
by kyrie_lrving1992 @ 2023-05-01 17:42:10
好的