E. The Lakes

题目:

输入
5
3 3
1 2 0
3 4 0
0 0 5
1 1
0
3 3
0 1 1
1 0 1
1 1 1
5 5
1 1 1 1 1
1 0 0 0 1
1 0 5 0 1
1 0 0 0 1
1 1 1 1 1
5 5
1 1 1 1 1
1 0 0 0 1
1 1 4 0 1
1 0 0 0 1
1 1 1 1 1

输出
10
0
7
16
21

 思路:

        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;
}

最后提交:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值