Skip to content

Commit 5121350

Browse files
committed
[BOJ] 1937 욕심쟁이 판다 (G3)
1 parent 96f2e46 commit 5121350

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

박예진/0주차/DFS/1937.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//https://www.acmicpc.net/problem/1937
2+
#include <iostream>
3+
#include <algorithm>
4+
#include <vector>
5+
#include <cstring>
6+
7+
using namespace std;
8+
9+
int n, ans;
10+
int dp[501][501];
11+
int arr[501][501];
12+
const int dx[4] = {-1, 0, 1, 0};
13+
const int dy[4] = {0, 1, 0, -1};
14+
15+
bool OOB(int x, int y) {
16+
return x < 0 || x >= n || y < 0 || y >= n;
17+
}
18+
19+
int dfs(int x, int y) {
20+
int &res = dp[x][y];
21+
if (res != -1) return res; // 메모이제이션
22+
res = 1;
23+
24+
for(int dir = 0; dir < 4; dir++){
25+
int nx = x + dx[dir];
26+
int ny = y + dy[dir];
27+
28+
if (OOB(nx, ny)) continue;
29+
if (arr[nx][ny] > arr[x][y]) {
30+
res = max(res, dfs(nx, ny) + 1);
31+
}
32+
}
33+
return res;
34+
}
35+
36+
int main(){
37+
ios_base::sync_with_stdio(false);
38+
cin.tie(NULL); cout.tie(NULL);
39+
40+
cin >> n;
41+
for(int i = 0; i < n; i++){
42+
for(int j = 0; j < n; j++){
43+
cin >> arr[i][j];
44+
}
45+
}
46+
47+
memset(dp, -1, sizeof(dp));
48+
for(int i = 0; i < n; i++){
49+
for(int j = 0; j < n; j++){
50+
// 모든 지점에서 시작
51+
ans = max(ans, dfs(i, j));
52+
}
53+
}
54+
cout << ans;
55+
}

0 commit comments

Comments
 (0)