1일1알

백준 1937번 욕심쟁이 판다 C++ 본문

알고리즘

백준 1937번 욕심쟁이 판다 C++

영춘권의달인 2022. 9. 4. 10:48

출처 : https://www.acmicpc.net/problem/1937

 

dfs와 메모이제이션 기법을 활용해서 풀었다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

#include <bitset>

using namespace std;
using int64 = long long;

int n;

int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };

vector<vector<int>> board;
vector<vector<int>> dp;

int Dfs(int row, int col) {
    int& val = dp[row][col];
    if (val != -1) return val;
    val = 1;
    for (int i = 0; i < 4; i++) {
        int nextRow = row + dRow[i];
        int nextCol = col + dCol[i];
        if (nextRow < 0 || nextRow >= n) continue;
        if (nextCol < 0 || nextCol >= n) continue;
        if (board[nextRow][nextCol] <= board[row][col]) continue;
        val = max(val, Dfs(nextRow, nextCol) + 1);
    }
    return val;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

    cin >> n;
    board = vector<vector<int>>(n, vector<int>(n));
    dp = vector<vector<int>>(n, vector<int>(n, -1));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> board[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            Dfs(i, j);
        }
    }
    int ans = 0;
    for (auto a : dp) {
        for (auto b : a) {
            ans = max(ans, b);
        }
    }
    cout << ans;
}

'알고리즘' 카테고리의 다른 글

백준 20291번 파일 정리 C++  (0) 2022.09.06
백준 2146번 다리 만들기 C++  (0) 2022.09.05
백준 9466번 텀 프로젝트 C++  (0) 2022.09.03
백준 1520번 내리막 길 C++  (0) 2022.09.02
백준 16197번 두 동전 C++  (0) 2022.09.01