代码拉取完成,页面将自动刷新
class Solution {
public:
int ret = 0, m = 0, n = 0, tempret = 0;
bool isLegal(vector<vector<int>>& grid, int m_, int n_) {
if (m_ < 0 || n_ < 0) return false;
if (m_ >= m || n_ >= n) return false;
if (!grid[m_][n_]) return false;
return true;
}
void DFS(vector<vector<int>>& grid, bool **vis, int nowm, int nown) {
tempret += grid[nowm][nown];
ret = tempret > ret ? tempret : ret;
if (isLegal(grid, nowm-1, nown) && !vis[nowm-1][nown]) {
vis[nowm-1][nown] = true;
DFS(grid, vis, nowm-1, nown);
vis[nowm-1][nown] = false;
}
if (isLegal(grid, nowm, nown-1) && !vis[nowm][nown-1]) {
vis[nowm][nown-1] = true;
DFS(grid, vis, nowm, nown-1);
vis[nowm][nown-1] = false;
}
if (isLegal(grid, nowm+1, nown) && !vis[nowm+1][nown]) {
vis[nowm+1][nown] = true;
DFS(grid, vis, nowm+1, nown);
vis[nowm+1][nown] = false;
}
if (isLegal(grid, nowm, nown+1) && !vis[nowm][nown+1]) {
vis[nowm][nown+1] = true;
DFS(grid, vis, nowm, nown+1);
vis[nowm][nown+1] = false;
}
tempret -= grid[nowm][nown];
}
int getMaximumGold(vector<vector<int>>& grid) {
m = grid.size(), n = grid[0].size();
bool** vis = new bool*[m];
for (int i = 0; i < m; i++) {
vis[i] = new bool[n];
for (int j = 0; j < n; j++)
vis[i][j] = false;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j]) {
tempret = 0;
vis[i][j] = true;
DFS(grid, vis, i, j);
vis[i][j] = false;
}
}
}
return ret;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。