* Computer Science/Algorithm

baekjun 11048. 이동하기

soicem 2018. 10. 29. 18:39

 O(3^n) dp문제 - 근데


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
#include <bits/stdc++.h>
 
using namespace std;
int N,M;
int maze[1001][1001];
int cache[1001][1001];
 
int findMaze(int i, int j){
    if(i == N && j == M){
        return maze[i][j];
    }
    if(i > N || j > M){
        return 0;
    }
    if(cache[i][j]!=-1){
        return cache[i][j];
    }
    int &ret = cache[i][j];
    int result = findMaze(i + 1, j);
    result = max(result, findMaze(i + 1, j + 1));
    result = max(result, findMaze(i, j + 1));
    return ret = maze[i][j] + esult;
}
 
int main(){
    cin >> N >> M;
    memset(cache, -1sizeof(cache));
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
            cin >> maze[i][j];
    cout << findMaze(11);
    return 0;
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
 
using namespace std;
int maze[1001][1001];
int d[1001][1001];
int N,M;
 
int main(){
    cin >> N >> M;
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
            cin >> maze[i][j];
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= M; j++){
            d[i][j] = max(d[i][j-1], d[i-1][j]) + maze[i][j];
            // d[i][j] + d[i+1][j+1] <= d[i][j] + d[i][j+1] + d[i+1][j+1] if maze[x][y] >=0
        }
    }
    cout << d[N][M] << endl;
    return 0;
}
 
cs

*d[i][j] + d[i+1][j+1] <= d[i][j] + d[i][j+1] + d[i+1][j+1] if maze[x][y] >=0


'* Computer Science > Algorithm' 카테고리의 다른 글

baekjun 1890. jump  (0) 2018.10.30
baekjun 2178. 미로탐색  (0) 2018.10.30
baekjun 바이러스  (0) 2018.10.29
baekjun 7569. 토마토2  (0) 2018.10.05
baekjun 7576. 토마토  (0) 2018.10.05