题目:![](https://img-blog.csdnimg.cn/7b836813c03f460fbec8cffbaf05e308.png)
|
|
思路:
BFS 或者 DFS 走一遍,这里都是只用一次数值,所以我们每当取完这个数值之后,都可以赋值为 0 ,就可以边走边初始化的操作,我就是没想到这个一直 TLE (;´༎ຶД༎ຶ`)
所以AC代码如下:
#include <iostream>
#include <queue>
#include <algorithm>
#include <unordered_map>
#define x first
#define y second
#define mk make_pair
#define umap unordered_map
#define endl '\n'
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int N = 1e4 + 10;
int n, m; // 湖泊图大小
umap<int, int>g[N]; // 湖泊图数值
int step;
inline void DFS(int x, int y)
{
if (!g[x][y])
return ;
step += g[x][y];
g[x][y] = 0;
for (int i = 0; i < 4; ++i)
{
int bx = x + dx[i];
int by = y + dy[i];
DFS(bx, by);
}
return ;
}
inline void solve()
{
int ans = 0;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> g[i][j];
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (g[i][j])
{
step = 0;
DFS(i, j);
ans = max(ans, step );
}
}
}
cout << ans << endl;
}
int main()
{
___G;
// freopen("a.txt", "r", stdin);
int _t;
cin >> _t;
while (_t--)
{
solve();
}
return 0;
}
最后提交: