BOJ : https://www.acmicpc.net/problem/1937
github : https://github.com/junho0956/Algorithm/blob/master/1937/1937/%EC%86%8C%EC%8A%A4.cpp
dfs 와 dp 를 이용하여 문제를 해결하였습니다.
dp[n][m] : (n,m) 에서 이동할 수 있는 최대 횟수를 저장하는 의미
lis 알고리즘과 관련된 문제인데 아마도 dfs 와 dp로 돌리는거와 마찬가지로
각 좌표마다 가장 길게 만들 수 있는 부분수열을 찾으라는 의미일 것이다(?) 결국 dfs 돌리시면 됩니다.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int, int> pii;
int n;
int arr[501][501];
bool visit[501][501];
int dp[501][501];
int my[4] = { -1,1,0,0 };
int mx[4] = { 0,0,1,-1 };
int solve(int y, int x, int pre) {
if (arr[y][x] <= pre) return 0;
int& res = dp[y][x];
if (res != -1) return res;
visit[y][x] = true;
int ans = 0;
for (int i = 0; i < 4; i++) {
int yy = y + my[i];
int xx = x + mx[i];
if (yy >= 0 && yy < n && xx >= 0 && xx < n && !visit[yy][xx]) {
ans = max(ans, solve(yy, xx, arr[y][x]) + 1);
}
}
visit[y][x] = false;
return res = ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
memset(dp, -1, sizeof(dp));
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans = max(ans, solve(i, j, 0));
}
}
cout << ans;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
복습 20.02.10
'algorithm > BOJ' 카테고리의 다른 글
BOJ 1463번 1로 만들기 (0) | 2019.04.07 |
---|---|
BOJ 1965번 상자넣기 (0) | 2019.04.07 |
BOJ 14003번 가장 긴 증가하는 부분수열5 (0) | 2019.04.06 |
BOJ 14002번 가장 긴 증가하는 부분수열4 (0) | 2019.04.06 |
가장 긴 증가하는 부분수열3(12738) - Junnnho (0) | 2019.04.06 |