Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 재귀
- 그리디 알고리즘
- 누적 합
- 다이나믹 프로그래밍
- 다익스트라
- XR Interaction Toolkit
- DFS
- 브루트포스
- 우선순위 큐
- 알고리즘
- ue5
- 백준
- c++
- Unreal Engine 5
- 문자열
- Team Fortress 2
- 유니온 파인드
- 수학
- 투 포인터
- 유니티
- 그래프
- 백트래킹
- 자료구조
- 구현
- BFS
- 정렬
- 스택
- 시뮬레이션
- 트리
- VR
Archives
- Today
- Total
1일1알
백준 1937번 욕심쟁이 판다 C++ 본문
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 |