Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2284}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지: DFS + DP
  • 🔹 어떤 방식으로 접근했는지

현재 위치를 기준으로 4방향을 탐색하면서, 현재보다 큰 값을 가진 곳에 방문한다.

방문 체크는 visited 대신 탐색된 가장 긴 경로를 저장하도록 하여 dp를 사용하였다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: $O(N^2)$
  • 이유:

이미 방문한 곳은 추가로 탐색되지 않으며, $N^2$ 형태의 board를 탐색하기 때문이다.

💻 구현 코드

#include <iostream>
#include <vector>

using namespace std;

int dy[4] = { 1, -1, 0, 0 };
int dx[4] = { 0, 0, 1, -1 };
int DFS(vector<vector<int>>& board, vector<vector<int>> &dp, int y, int x) {
    if (dp[y][x])
        return dp[y][x];
    dp[y][x] = 1; // 방문체크

    int maxV = 1;
    for (int dir = 0; dir < 4; dir++) {
        int ny = y + dy[dir];
        int nx = x + dx[dir];

        if (ny < 0 || nx < 0 || ny >= board.size() || nx >= board[0].size())
            continue;
        if (board[y][x] >= board[ny][nx])
            continue;

        maxV = max(maxV, DFS(board, dp, ny, nx) + 1);
    }

    return dp[y][x] = maxV;
}

void print(vector<vector<int>> dp) {
    int N = dp.size();
    for (int y = 0; y < N; y++) {
        for (int x = 0; x < N; x++) {
            cout << dp[y][x] << " " ;
        }
        cout << endl;
    }
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int N;
    cin >> N;

    vector<vector<int>> board(N, vector<int>(N));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> board[i][j];
        }
    }

    vector<vector<int>> dp(N, vector<int>(N));
    int answer = 0;
    for (int y = 0; y < N; y++) {
        for (int x = 0; x < N; x++) {
            answer = max(answer, DFS(board, dp, y, x));
            // print(dp);
            // cout << "\n";
        }
    }



    cout << answer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260108 : 코딩테스트

2 participants