알고리즘
백준 1937번 욕심쟁이 판다 C++
영춘권의달인
2022. 9. 4. 10:48

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