社区讨论
bfs MLE 80pts求助
P1596[USACO10OCT] Lake Counting S参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m63d8njh
- 此快照首次捕获于
- 2025/01/19 16:39 去年
- 此快照最后确认于
- 2025/11/04 11:17 4 个月前
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,cnt;
int mp[110][110],f[110][110];
int mov[8][2] = {{0,1},{0,-1},{1,1},{1,0},{1,-1},{-1,1},{-1,0},{-1,-1}};
char tmp;
void bfs(int x,int y)
{
queue<pair<int,int> > q;
q.push({x,y});
while (!q.empty())
{
int nx = q.front().first,ny = q.front().second;
q.pop();
f[nx][ny] = 2;
for (int i = 0;i < 8;i++)
{
int xx = nx + mov[i][0],yy = ny + mov[i][1];
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && f[xx][yy] == 1)
{
q.push({xx,yy});
}
}
}
}
signed main()
{
cin >> n >> m;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
cin >> tmp;
mp[i][j] = f[i][j] = (tmp == 'W' ? 1 : 0);
}
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
if (f[i][j] == 1)
{
bfs(i,j);
cnt++;
}
}
}
cout << cnt;
return 0;
}
交上去MLE两个点
回复
共 0 条回复,欢迎继续交流。
正在加载回复...