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

12-wnsmir #51

Merged
merged 1 commit into from
Jan 9, 2025
Merged

12-wnsmir #51

merged 1 commit into from
Jan 9, 2025

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Jan 3, 2025

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

์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค p152์—์„œ ๋ฐœ์ทŒํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ : ๋ฏธ๋กœํƒˆ์ถœ
N*M์˜ map์ด ์•„๋ž˜์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์ฃผ์–ด์ง€๊ณ , 1์€ ์ง€๋‚˜๋‹ค๋‹ ์ˆ˜ ์žˆ๋Š”๊ธธ, 0์€ ๋ฒฝ์ž…๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ ๋์œผ๋กœ ๊ฐ€๊ธฐ๊นŒ์ง€ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.

์ž…๋ ฅ์˜ˆ์‹œ
5 6
101010
111111
000001
111111
111111

์ถœ๋ ฅ์˜ˆ์‹œ
10

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

40min

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

N M์„ split์œผ๋กœ ๋ฐ›์•„์ค๋‹ˆ๋‹ค. (๋„์›Œ์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„)
์ดํ›„ ๊ทธ๋ž˜ํ”„๋Š” ๋„์›Œ์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„๋˜์–ด์žˆ์ง€ ์•Š๊ธฐ ๋–ผ๋ฌธ์— ๊ทธ๋ƒฅ input์œผ๋กœ ๋ฐ›์•„์ค๋‹ˆ๋‹ค.

dx dy๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š”๋ฐ, for๋ฌธ์œผ๋กœ i๋ฅผ 4๋ฒˆ ๋Œ๋ ค๊ฐ€๋ฉฐ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์‹คํ˜„ํ•˜๊ธฐ์œ„ํ•จ ์ž…๋‹ˆ๋‹ค.

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

for i in range(4):
   nx = x + dx[i]
   ny = y + dy[i]

์ €๋Š” ์ฃผ๋กœ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ดํ›„ BFS๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฒฝ์ด๊ฑฐ๋‚˜ ์ง€ํ˜•์„ ๋„˜์–ด๊ฐ€๋Š” ์ขŒํ‘œ๋ผ๋ฉด pass
์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•œ ๊ธธ์ด 1์ด๋ผ๋ฉด ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๋ผ ํŒ๋‹จํ•˜๊ณ  ์ด์ „ ๊ธธ์˜ ๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ฉ๋‹ˆ๋‹ค.

bfs๊ฐ€ ํ•œ๋‹จ๊ณ„์”ฉ ๊ธธ์„ ๋„“ํ˜€๊ฐ€๊ธฐ ๋–„๋ฌธ์— ์ตœ๋‹จ๊ฑฐ๋ฆฌ์— ์œ ์šฉํ•˜๋‹ค ํŒ๋‹จ๋˜์–ด bfs๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. (๊ฐ€์ค‘์น˜๋„ 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.

๋ฏธ๋กœ ์ฐพ๊ธฐ ์˜ค๋žœ๋งŒ์— ํ•ด๋ด์„œ ์žฌ๋ฐŒ๋„ค์š”! ์ €๋„ queue๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ฝค ๋นจ๋ฆฌ ์™„์„ฑํ–ˆ์—ˆ๋Š”๋ฐ... ์ŠคํŽ˜์ด์Šค๋กœ ๊ตฌ๋ถ„๋˜์ง€ ์•Š๋Š” ์ž…๋ ฅ์„ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผํ•ด์„œ...
cin ์ด ์ธ์‹์„ ๋ชปํ•ด์„œ ์ถœ๋ ฅ์ด ์•ˆ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹
์ €๋Š” ์ œ ๋กœ์ง์ด ์ž˜๋ชป๋˜์„œ ๋ฌดํ•œ๋ฃจํ”„์ธ ์ค„ ์•Œ๊ณ  ํ•œ์ฐธ ๊ณ ๋ฏผํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

(๊ทธ๋ฆฌ๊ณ  ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋“ฑ์€ ๋ฌด์‹œํ–ˆ์Šต๋‹ˆ๋‹ค!)

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

#define WALL 0
#define PATH 1
#define VISITED 2

using namespace std;

int N, M;
int offset[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

bool isMoveable(vector<vector<int>>& maze, int x, int y) {
    return 0 <= x && x < M && 0 <= y && y < N && maze[y][x] == PATH;
}

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

    vector<vector<int>> maze(N, vector<int>(M));
    for (int y = 0; y < N; y++) {
        for (int x = 0; x < M; x++) {
            cin >> maze[y][x];
        }
    }

    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));
    maze[0][0] = VISITED;

    int minDistance = 1;
    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size; i++) {
            int y = q.front().first;
            int x = q.front().second;
            q.pop();

            if (x == M - 1 && y == N - 1) {
                cout << minDistance;
                return 0;
            }

            for (int dir = 0; dir < 4; dir++) {
                int y_ = y + offset[dir][0];
                int x_ = x + offset[dir][1];

                if (isMoveable(maze, x_, y_)) {
                    q.push(make_pair(y_, x_));
                    maze[y_][x_] = VISITED;
                }
            }
        }
        minDistance++;
    }

    cout << -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.

์˜ค๋žœ๋งŒ์— ๋ฏธ๋กœ์ฐพ๊ธฐ ๋ฌธ์ œ ์ž˜ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋กœ์ฐพ๊ธฐ๋งŒํผ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ‚ฌ์„ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ ์œ ํ˜•์€ ๋˜ ์—†๋Š”๊ฒƒ ๊ฐ™๋„ค์š”.

๋‚จ๋“ค์ด bfs๋กœ ํ’€๊ฑฐ ๊ฐ™์•„ dfs๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ตœ๋‹จ ๊ธธ์ด๋ฅผ ๊ตฌํ•  ๋•Œ์—” dfs๊ฐ€ ํšจ์œจ์ ์ด์ง€ ์•Š์œผ๋‚˜ ๋ญ ์ง€๊ธˆ์€ ๊ทธ๋Ÿฐ๊ฑด ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์•„์„œ dfs๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•˜๊ณ  ์ •๋‹ต์ด ๋‚˜์˜ค์ง€ ์•Š์•„์„œ ํ•œ์ฐธ์„ ๊ณ ๋ฏผํ•˜๋‹ค ๊ฐ•๋ž˜๋‹˜์ฒ˜๋Ÿผ ์ž…๋ ฅ ๋ฌธ์ œ์˜€์Œ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค..

bool canMove(int x, int y) {
    return 0 <= x && x < m && 0 <= y && y < n
           && map[y][x] == 1;
}

void dfs(int x, int y, int cnt) {
    if (x == m - 1 && y == n - 1) {
        minimum = min(minimum, cnt);
        return;
    }

    for (auto &dir: offset) {
        int nx = x + dir[0];
        int ny = y + dir[1];
        if (canMove(nx, ny)) {
            map[ny][nx] = 2;
            dfs(nx, ny, cnt + 1);
            map[ny][nx] = 1;
        }
    }
}

@kokeunho
Copy link
Collaborator

kokeunho commented Jan 8, 2025

์ €๋„ BFS๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
map ์ž…๋ ฅ ๋ฐ›๋Š” ๋ถ€๋ถ„์ด ํ‰์†Œ ํ’€๋˜ ๋ฌธ์ œ๋ž‘ ์กฐ๊ธˆ ๋‹ฌ๋ผ์„œ
์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
ํ•ญ์ƒ nextInt()๋กœ ์ •์ˆ˜๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•˜๋Š”๋ฐ
ํ•ด๋‹น ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋„์›Œ์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ
nextLine()์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ  charAt(j) (j๋ฒˆ์งธ ๋ฌธ์ž์—ด)๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.
์ €๋„ ๋‹ค์Œ ๋ฌธ์ œ ๋ฏธ๋กœ ์ฐพ๊ธฐ ํ•œ๋ฒˆ ํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค
์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค.

java code
import java.util.*;

public class Main {
    static int n, m;
    static int[][] map;
    static int[][] distance;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        map = new int[n][m];
        distance = new int[n][m];

        sc.nextLine();

        for (int i = 0; i < n; i++) {
            String line = sc.nextLine();
            for (int j = 0; j < m; j++) {
                map[i][j] = line.charAt(j) - '0';
                distance[i][j] = -1;
            }
        }
        bfs(0, 0);

        System.out.println(distance[n-1][m-1]);

    }

    public static void bfs(int sx, int sy) {
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{sx, sy});
        distance[sx][sy] = 1;

        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int x = current[0];
            int y = current[1];

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

                if (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] == 1 && distance[nx][ny] == -1) {
                    distance[nx][ny] = distance[x][y] + 1;
                    queue.offer(new int[]{nx, ny});
                }
            }
        }
    }
}

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.

4 participants