DP를 사용하지 않아도 풀리는 문제인거 같으나 나는 DP를 활용하여 풀었다.


1. d배열에 각각 위치에서 이동할 수 있는 최대값을 저장한다.


2. 

이동가능할 때 :

d[ax][ay]==0 ? (ax,ay)지점에 한번도 방문하지 않음 -> 방문한다 ( dfs(ax,ay) )


3. d[ax][ay]와 값 비교하여 d[x][y] 를 최대값으로 만든다.


4. 미방문 지점에 대해 반복한다. 



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <algorithm>
using namespace std;
 
int t, n;
int map[1002][1002];
int d[1002][1002];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
 
void dfs(int x, int y) {
    d[x][y] = 1;
 
    for (int i = 0; i < 4; i++) {
        int ax = x + dx[i];
        int ay = y + dy[i];
 
        if (map[ax][ay] == 0continue;
 
        if (map[ax][ay] == map[x][y] + 1) {
            if (d[ax][ay] == 0) dfs(ax, ay);
            d[x][y] = max(d[x][y], d[ax][ay] + 1);
        }
    }
}
 
void get_result(int tc) {
    int cnt = 0;
    int num = 987654321;
 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (cnt < d[i][j]) {
                cnt = d[i][j];
                num = map[i][j];
            }
            else if (cnt == d[i][j]) {
                num = min(num, map[i][j]);
            }
        }
    }
 
    cout << "#" << tc << " " << num << " " << cnt << "\n";
}
 
void printAll() {
    cout << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << d[i][j] << " ";
        }
        cout << endl;
    }
}
 
int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
 
    cin >> t;
    for (int tc = 1; tc <= t; tc++) {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> map[i][j];
                d[i][j] = 0;
            }
        }
 
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (d[i][j] == 0) dfs(i, j);
            }
        }
        get_result(tc);
 
        //printAll();
    }
    return 0;
}
cs


'SWEA::문제풀이' 카테고리의 다른 글

4112 이상한 피라미드 탐험  (0) 2018.04.12
2383 점심 식사시간  (4) 2018.04.11
4014 활주로 건설  (2) 2018.04.09
3304 최장 공통 부분 수열  (0) 2018.04.08
2382 미생물 격리  (2) 2018.03.29

+ Recent posts