问:标签有dfs,蒟蒻用dfs哪里出错
查看原帖
问:标签有dfs,蒟蒻用dfs哪里出错
297798
FANTA5TlC楼主2020/11/26 21:21

code

#include <bits/stdc++.h>
using namespace std;
int a[105][105];
bool vis[105][105], gt[105][105];
int n, m;
int maxs = -1;
void dfs(int x, int y, int h, bool isget){
	if (x < 1 || y < 1 || x > m || y > n) return;
	if (gt[x - 1][y] || gt[x + 1][y] || gt[x][y - 1] || gt[x][y + 1]) return;
	if (vis[x][y] == true) return;
	int ans = 0;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			if (gt[i][j] == true){
				ans += a[i][j];
			}
	maxs = max(ans, maxs);
	vis[x][y] = false;
	dfs(x + 1, y, h + a[x][y], gt[x][y] = isget = true);
	dfs(x + 1, y, h, gt[x][y] = isget = false);
	dfs(x - 1, y, h + a[x][y], gt[x][y] = isget = true);
	dfs(x - 1, y, h, gt[x][y] = isget = false);
	dfs(x, y + 1, h + a[x][y], gt[x][y] = isget = true);
	dfs(x, y + 1, h, gt[x][y] = isget = false);
	dfs(x, y - 1, h + a[x][y], gt[x][y] = isget = true);
	dfs(x, y - 1, h, gt[x][y] = isget = false);
	vis[x][y] = true;
}
int main(){
	cin >> m >> n;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			cin >> a[i][j];
	dfs(1, 1, 0, false);
	cout << maxs << endl;
}

2020/11/26 21:21
加载中...