明白了,我嘚了,没看懂题
by Luochen567 @ 2023-11-05 11:24:01
@[Luochen567](/user/1087205)
给你一组hack:
2 2
. W
. .
应该输出:1
输出:0
by zcy_jake @ 2023-11-05 11:33:32
@[Luochen567](/user/1087205)
```cpp
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int n, m,ans=0;
int zw_x[8] = {-1,-1,-1,0,1,1,1,0};
int zw_y[8] = {-1,0,1,1,1,0,-1,-1};
char a[105][105] = { 0 };
struct pos {
int x;
int y;
};
queue<pos> q;
void bfs() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '.') {
continue;
}else{
ans++;
pos ls;
ls.x = i;
ls.y = j;
q.push(ls);
while (!q.empty()) {
pos ls2 = q.front();
q.pop();
a[ls2.x][ls2.y] = '.';
for (int i = 0; i < 8; i++) {
if (a[ls2.x + zw_x[i]][ls2.y + zw_y[i]] == 'W') {
pos ls3;
ls3.x = ls2.x + zw_x[i];
ls3.y = ls2.y + zw_y[i];
q.push(ls3);
}
}
}
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
bfs();
cout << ans;
return 0;
}
```
这样就没有WA了。
望互关(QWQ)
by zcy_jake @ 2023-11-05 11:38:32