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

15-kokeunho #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

15-kokeunho #58

wants to merge 1 commit into from

Conversation

kokeunho
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

[BOJ] ๊ฒฉ์ž์ƒ์˜ ๊ฒฝ๋กœ https://www.acmicpc.net/problem/10164

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

K๊ฐ€ 0 (๊ฑฐ์ณ์•ผ ํ•  ์ง€์ ์ด ์—†๋Š”)์ธ ๊ฒฝ์šฐ์™€ K๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

K๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์ง€์ ์˜ ์ขŒํ‘œ๋ฅผ ์ฐพ๊ณ  ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ๊ฑฐ์ณ์„œ
์ตœ์ข… ๋ชฉ์ ์ง€์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์˜ค๋ฅธ์ชฝ์ด๋‚˜ ์•„๋ž˜ ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ
findPath(0, 0, midX, midY) * findPath(midX, midY, N-1, M-1)์ด
๊ณง ์ตœ์ข… ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ์˜ ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

K๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๊ทธ๋ƒฅ findPath(0,0,N-1,M-1) ์ž…๋‹ˆ๋‹ค.

findPath ํ•จ์ˆ˜์—์„œ๋Š”
์šฐ์„  ๋ฐฐ์—ด์˜ ๊ฐ’๋“ค์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ฃผ๊ณ 
์‹œ์ž‘ ์ง€์ ๋ถ€ํ„ฐ ๋ชฉํ‘œ ์ง€์ ๊นŒ์ง€ ์šฐ, ํ•˜๋กœ ์ด๋™ํ•˜๋ฉฐ ํ•ด๋‹น ์ง€์ ๊นŒ์ง€์˜ ๊ฒฝ๋กœ ์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

         for (int i = sx; i <= ex; i++) {
            for (int j = sy; j <= ey; j++) {
                if (i > sx) path[i][j] += path[i-1][j];
                if (j > sy) path[i][j] += path[i][j-1];
            }
        }

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

2์ฐจ์› ๋ฐฐ์—ด์—์„œ K ๋ฒˆ์งธ ์š”์†Œ์˜ ์ขŒํ‘œ๋ฅผ ์ฐพ์„ ๋•Œ, ์ฒ˜์Œ์—๋Š” ๋ฐฐ์—ด์— ๊ฐ’์„
1์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉฐ ๋„ฃ์–ด์ฃผ๊ณ  K์™€ ๊ฐ™์€ ๊ฐ’์„ ๋„ฃ๊ฒŒ ๋  ๋•Œ ์ขŒํ‘œ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•˜๋Š” ์‹์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ 2์ฐจ์› ๋ฐฐ์—ด์ด ํ•˜๋‚˜ ๋” ํ•„์š”ํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ์ขŒํ‘œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

        int mx = 0, my = 0;
        if (K != 0) {
            mx = (K-1) / M;
            my = (K-1) % M;
        }

Comment on lines +34 to +38
for (int i = 0; i <= ex; i++) {
for (int j = 0; j <= ey; j++) {
path[i][j] = 0;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด ๋ถ€๋ถ„์„ ๋ณด๊ณ  ๊ถ๊ธˆํ•œ ์ ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค!

path ๋ฐฐ์—ด์€ findPath์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š”๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ์ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋งค๋ฒˆ ๋ถ€๋ถ„์ ์ธ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”
๊ทธ๋ƒฅ ํ•จ์ˆ˜ ์Šคํƒ์—์„œ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?

int[][] path = new int[N][M];  // ๋ชจ๋“  ๊ฐ’์ด 0์œผ๋กœ ์ž๋™ ์ดˆ๊ธฐํ™”๋จ

๋˜๋Š” ๋ฒ”์œ„๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ง€์ •ํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?

for (int i = sx; i <= ex; i++) {
    for (int j = sy; j <= ey; j++) {
        path[i][j] = 0;
    }
}

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

์ค‘ํ•™์ƒ? ๊ณ ๋“ฑํ•™์ƒ? ๋•Œ ์ˆ˜ํ•™๋ฌธ์ œ๋กœ ์ž์ฃผ ๋‚˜์™”๋˜ ๋ฌธ์ œ ๊ฐ™๋„ค์š”
๋•๋ถ„์— ๋ฌธ์ œ ์ดํ•ด๋Š” ์‰ฌ์› ์Šต๋‹ˆ๋‹ค

์ €๋Š” 1 to K, K to N*M ์„ ๊ณฑํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
1 to K๋ฅผ ๋จผ์ € ์ง„ํ–‰ํ•˜๊ณ , K์ขŒํ‘œ๊ฐ€ ์‹œ์ž‘์ด๋ผ ์ƒ๊ฐํ•˜๊ณ  ๋‹ค์‹œ K to N*M์„ ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
( ๋ฐ˜๋ณต๋˜๋Š” ๋กœ์ง์ด ์žˆ์–ด์„œ ํ•จ์ˆ˜ํ™” ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ passํ–ˆ์Šต๋‹ˆ๋‹ค.. ใ…Žใ…Ž)

์กฐ๊ธˆ ์–ด๋ ค์› ๋˜ ์ ์€ K ์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.

int K_x = K % M - 1;
int K_y = K / M;
if (K == 0) {
    K_x = 0;
    K_y = 0;
}
else if (K_x < 0){
    K_x = M - 1;
    K_y--;
}

์กฐ๊ฑด ๊ฒ€์‚ฌ๋„ ๋งŽ๊ณ  ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ๋๋„ค์š”..
๊ทผํ˜ธ๋‹˜์€ ๊น”๋”ํ•œ ๋กœ์ง์œผ๋กœ ํ•œ๋ฒˆ์— ์ž˜ ์ฒ˜๋ฆฌํ•˜์‹  ๊ฒƒ ๊ฐ™๋„ค์š” ! ๐Ÿ‘

CPP CODE
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int N, M, K;
    cin >> N >> M >> K;

    // find K's position
    int K_x = K % M - 1;
    int K_y = K / M;
    if (K == 0) {
        K_x = 0;
        K_y = 0;
    }
    else if (K_x < 0){
        K_x = M - 1;
        K_y--;
    }

    vector<vector<int>> board(N, vector<int>(M, 0));
    // 1 to K
    for (int y = 0; y <= K_y; y++) {
        for (int x = 0; x <= K_x; x++) {
            if (x == 0 || y == 0) board[y][x] = 1;
            else board[y][x] += board[y][x - 1] + board[y - 1][x];
        }
    }

    // K to N*M
    for (int y = K_y; y < N; y++) {
        for (int x = K_x; x < M; x++) {
            if (x == K_x || y == K_y) board[y][x] = board[K_y][K_x];
            else board[y][x] += board[y][x - 1] + board[y - 1][x];
        }
    }

    cout << board[N - 1][M - 1];

    return 0;
}

์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค~

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

์ €๋Š” dfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋Š”๋ฐ dp๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒ๊ฐ์น˜๋„ ๋ชปํ–ˆ๋„ค์š”. ์˜ˆ์ „์— ๋ฐฐ์› ๋˜ ๋ฌธ์ œ๋ฅผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ๋ผ ํŒ๋‹จํ•ด๋ฒ„๋ ค์„œ ๋‹ค๋ฅธ ์ƒ๊ฐ์€ ํ•˜์ง€๋„ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”. ๋ฌธ์ œ๋ฅผ ์ข€ ๋” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฉด์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ์–ด์š”

์‚ฌ์‹ค dfs๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์—๋„ ์–ด๋ ค์šด ์ ์ด ๋งŽ์•˜์Šต๋‹ˆ๋‹ค. ์˜ค๋žœ๋งŒ์— ๋ฏธ๋กœ๋ฅผ dfs๋กœ ํ’€๋ ค ํ•˜๋‹ค๋ณด๋‹ˆ base case์— ํ•ด๋‹นํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ž˜๋ชป ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ base case๋ฅผ ์„ค์ •ํ•˜์˜€๊ณ  k์˜ ์œ„์น˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์—์„œ๋„ ์˜ค๋ฅ˜๊ฐ€ ๋งŽ์•˜์–ด์„œ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ €๋ฒˆ์ฒ˜๋Ÿผ ๋ฐฑ์ค€์—์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋Š”๋ฐ์š”. ์ข€ ๋ง‰๋ง‰ํ•˜๋”๊ตฐ์š”.. ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ฅด๋‹ˆ ํ•ด๋‹น ์–ธ์–ด์— ๋Œ€ํ•œ ๋ฌธ๋ฒ•์„ ์™„๋ฒฝํžˆ ์ˆ™์ง€ํ•ด์•ผํ•˜๊ธฐ์— ๋ฌธ์ œ๊ฐ€ ๋” ์–ด๋ ต๊ฒŒ ๋Š๊ปด์กŒ๋˜๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ๊ดœ์ฐฎ์€ ์—ฐ์Šต ๋ฐฉ๋ฒ•์ด๋ผ ์ƒ๊ฐํ•˜์—ฌ ์ข…์ข… ํ•ด๋ณด๋Š”๊ฑธ ์ถ”์ฒœ๋“œ๋ ค์š”!

์ œ ์™„์„ฑ๋œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์‹œ๊ฐ„์ดˆ๊ณผ๋Š” ์—†์—ˆ๊ณ  ์ ์ˆ˜๋„ 100์  ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์•„๋งˆ N๊ณผ M๊ฐ’์ด ๋„ˆ๋ฌด ์ž‘์•„์„œ ๊ทธ๋Ÿฐ๊ฑฐ ๊ฐ™์•„์š”.

code
//
// Created by ๊น€๊ท ํ˜ธ on 2025. 1. 17..
//
#include <iostream>
#include <vector>
using namespace std;

int n, m, k;
int offset[2][2] = {{1, 0}, {0, 1}};
vector<vector<int> > map; //[n][m]
vector<vector<bool> > visited;

int dfs(int xs, int ys, int xd, int yd) {
    if (xs == xd && ys == yd) return 1;
    if (xs < 0 || ys < 0 || xs >= m || ys >= n) return 0;
    if (visited[ys][xs]) return 0;

    int cnt = 0;
    visited[ys][xs] = true;
    for (auto &dir: offset) {
        int nx = xs + dir[0];
        int ny = ys + dir[1];
        cnt += dfs(nx, ny, xd, yd);
    }
    visited[ys][xs] = false;
    return cnt;
}

int main() {
    cin >> n >> m >> k;
    map = vector<vector<int> >(n, vector<int>(m));
    visited = vector<vector<bool> >(n, vector<bool>(m, false));

    int xt = 0, yt = 0;
    if (k != 0) {
        xt = (k - 1) % m;
        yt = (k - 1) / m;
    }

    int fir = dfs(0, 0, xt, yt);
    int sec = dfs(xt, yt, m - 1, n - 1);
    cout << fir * sec;
    return 0;
}

Base automatically changed from 14-kokeunho to main January 21, 2025 06:27
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.

3 participants