寻宝图(CCCC天梯赛L2-048)-BFS
作者:
无苦邪
,
2024-03-17 15:22:06
,
所有人可见
,
阅读 49
寻宝图 bfs
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int N, M, island, preciosu, p;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
string dao_map[100005];
typedef pair<int, int> pll;
queue<pll>q;
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) cin >> dao_map[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (dao_map[i][j] != '0') {
++island;
q.push({ i,j });
// p 判断是否有宝藏
p = 0;
if (dao_map[i][j] >= '2') p = 1;
dao_map[i][j] = '0';
while (q.size()) {
auto cur = q.front();
q.pop();
int cur_x = cur.first; int cur_y = cur.second;
for (int k = 0; k < 4; k++) {
int X = cur_x + dx[k];
int Y = cur_y + dy[k];
if ( X >= 0 && X < N && Y >= 0 && Y < M && dao_map[X][Y] >= '1') {
if (dao_map[X][Y] >= '2') {
p = 1;
}
q.push({ X,Y });
dao_map[X][Y] = '0';
}
}
}
if (p) preciosu++;
}
}
}
cout << island << ' ' << preciosu;
return 0;
}