Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpp] 7차 Softeer 정기 역량 진단 2번 문제 - 순서대로 방문하기 #64

Merged
merged 2 commits into from
Sep 2, 2023

Conversation

minjae9610
Copy link
Member

🌁 Background

2023년 7차 Softeer 정기 역량 진단

👩‍💻 Contents

[7차 Softeer 정기 역량 진단] 2번 - 순서대로 방문하기

image
image
image
image
image
image
image
image

#include<iostream>

using namespace std;

struct Pos {
    int row, col;
};

struct Directions {
    int drow, dcol;
};

Directions direc[4] = {
  {-1, 0},
  {0, -1},
  {1, 0},
  {0, 1}
};

bool visited[4][4];
int pos_map[4][4];

int dfs(int n, int end_idx, Pos cur_pos, int pos_idx = 2) {
    int cur_idx = pos_map[cur_pos.row][cur_pos.col];
    if (cur_idx == end_idx && cur_idx == pos_idx)
        return 1;
    else if (cur_idx == pos_idx)
        ++pos_idx;
    else if (cur_idx)
        return 0;

    int count = 0;
    for (auto d : direc) {
        Pos next_pos;
        next_pos.row = cur_pos.row + d.drow;
        next_pos.col = cur_pos.col + d.dcol;

        if (next_pos.row < 0 || next_pos.row >= n || next_pos.col < 0 || next_pos.col >= n || visited[next_pos.row][next_pos.col])
            continue;

        visited[next_pos.row][next_pos.col] = true;
        count += dfs(n, end_idx, next_pos, pos_idx);
        visited[next_pos.row][next_pos.col] = false;
    }
    return count;
}

int main(int argc, char** argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            cin >> visited[i][j];

    int row, col;
    Pos start_pos;
    cin >> row >> col;
    visited[row - 1][col - 1] = true;
    start_pos.row = row - 1;
    start_pos.col = col - 1;

    for (int i = 2; i <= m; ++i) {
        Pos pos;
        cin >> row >> col;
        pos_map[row - 1][col - 1] = i;
        pos.row = row - 1;
        pos.col = col - 1;
    }

    cout << dfs(n, m, start_pos) << '\n';

    return 0;
}

📝 Review Note

dfs 문제에 특정 지점들을 순서대로 방문하는 조건이 추가된 문제이다.
해당 지점들을 알맞는 순서에 방문하지 않으면 백트래킹을 통해 시간을 단축시킬 수 있다.

@minjae9610 minjae9610 requested a review from a team as a code owner August 25, 2023 06:04
@minjae9610 minjae9610 requested review from kgh2120 and WhalesBob and removed request for a team August 25, 2023 06:04
@Sumin-Kim-dev
Copy link
Contributor

전 그냥 DFS+백트래킹 시간복잡도 계산에 실패해서 그냥 안전하게 가자! 하고 DP까지 접목했었는데, 완성하고 돌려보니까 생각보다 경우의 수가 적더라고요. 나는 왜 생고생을 했는가...
복기 잘 봤습니다!

@minjae9610
Copy link
Member Author

minjae9610 commented Aug 28, 2023

전 그냥 DFS+백트래킹 시간복잡도 계산에 실패해서 그냥 안전하게 가자! 하고 DP까지 접목했었는데, 완성하고 돌려보니까 생각보다 경우의 수가 적더라고요. 나는 왜 생고생을 했는가...
복기 잘 봤습니다!

@Sumin-Kim-dev 네네 시간복잡도가 커서 맵이 조금만 커져도 경우의 수가 기하급수적으로 커지지만 맵이 작아서 백트래킹만 적용해줘도 어느정도 경우의 수가 잡히더라구요. 수고 많으셨습니다. :)

Copy link
Contributor

@WhalesBob WhalesBob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

소프티어 문제 푸시느라 고생 많으셨습니다! 그냥 완전탐색으로 해도 시간초과가 나지 않음에도 불구하고, 백트래킹까지 마저 적용해서 문제를 푸시는 부분은, 저도 배워 가야 할 것 같습니다! 통과하셨을 것 같아요!

Copy link
Member

@kgh2120 kgh2120 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다! 현대 코테는 이제 든든하시겠어요 ㅎㅎ

@minjae9610 minjae9610 merged commit 3881c37 into main Sep 2, 2023
1 check passed
@minjae9610 minjae9610 deleted the algo/minjae9610/softeer-HSAT-7-2 branch September 2, 2023 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants