Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int dx[4] = {-1, 0, 1, 0};
- const int dy[4] = {0, 1, 0, -1};
- int getGold(int r, int c, vector<vector<int> > &matrix){
- if(r < 0 || r >= matrix.size() || c < 0 || c >= matrix[0].size() || matrix[r][c] == 0){
- return 0;
- }
- int currCellGold = matrix[r][c];
- matrix[r][c] = 0;
- for(int i = 0; i < 4; i++){
- currCellGold += getGold(r + dx[i], c + dy[i], matrix);
- }
- return currCellGold;
- }
- int main() {
- // your code goes here
- int n, m;
- cin >> n >> m;
- vector<vector<int> > matrix(n, vector<int>(m, 0));
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cin >> matrix[i][j];
- }
- }
- int maxGold = 0;
- int maxInAPath = 0;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- if(matrix[i][j] != 0){
- // maxGold += getGold(i, j, matrix);
- int recGold = 0;
- recGold = getGold(i, j, matrix);
- maxGold += recGold;
- maxInAPath = max(maxInAPath, recGold);
- }
- }
- }
- cout << maxGold << '\n';
- cout << maxInAPath << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement