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

18-kokeunho #73

Merged
merged 1 commit into from
Feb 28, 2025
Merged

18-kokeunho #73

merged 1 commit into from
Feb 28, 2025

Conversation

kokeunho
Copy link
Collaborator

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

[BOJ] ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ https://www.acmicpc.net/problem/14503

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

1h

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

์ €๋ฒˆ์— ์ค€์šฉ๋‹˜ pr์ด์—ˆ๋˜๊ฐ€์š”.
๊ฑฐ์˜ ๋™์ผํ•œ ๋ฌธ์ œ์ธ๋ฐ ํ•œ๋ฒˆ ๋‹ค์‹œ ์ƒ๊ธฐํ•ด๋ณด๊ณ ์ž ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.
๋ฐฉํ–ฅ ๊ด€๋ จํ•ด์„œ๋Š” ๊ธˆ๋ฐฉ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ
์ „์ง„, 4๋ฐฉํ–ฅ ํƒ์ƒ‰, ํ›„์ง„ ๊ตฌํ˜„์— ์กฐ๊ธˆ ํ—ค๋งธ์Šต๋‹ˆ๋‹ค.
์•„๋งˆ ๋‹ค๋ฅธ ๋ถ„๋“ค์€ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค.

1. ์šฐ์„  ํ˜„์žฌ ์œ„์น˜์˜ cleaned๋ฅผ true๋กœ ๋ฐ”๊ฟ”์ค€ ํ›„ count++ํ•ฉ๋‹ˆ๋‹ค.
2. 4๋ฐฉํ–ฅ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ (turnLeft)ํ•˜๋ฉฐ
๊ทธ ์•ž ์นธ์ด ์ „์ง„ํ•  ์ˆ˜ ์žˆ๊ณ  ์ฒญ์†Œ ๋˜์ง€ ์•Š์€ ์นธ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
3. ์ „์ง„ํ•œ๋‹ค๋ฉด moved๋ฅผ true๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ๋‹ค์‹œ 1~2๋ฒˆ์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
4. ์ œ์ž๋ฆฌ์— ๋จธ๋ฌผ๋Ÿฌ ์žˆ๋Š” ๊ฒฝ์šฐ(moved == false) ํ›„์ง„ํ•˜๊ณ  ํ›„์ง„ ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด
while๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๊ณ  count ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

PCCP ์—ฐ์Šต ๊ฒธ ์•ž์œผ๋กœ ๊ตฌํ˜„ ๋ฌธ์ œ ์œ„์ฃผ๋กœ ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

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

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.

๋จธ๋ฆฌ๋กœ๋Š” ์‰ฝ๊ฒŒ ๊ทธ๋ ค์กŒ๋Š”๋ฐ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๊ฐ€ ์‚ด์ง ๋นก์„ธ๋„ค์š”.

๋ง์”€ํ•˜์‹ ๋Œ€๋กœ ์ €๋ฒˆ์— ์ค€์šฉ๋‹˜ ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•ด์„œ ๋ฌธ์ œ ์ ‘๊ทผ์€ ์‰ฝ๊ฒŒ ํ•œ ๊ฒƒ ๊ฐ™์•„์š”.
์กฐ๊ธˆ ์–ด๋ ค์šด ์ ์ด์—ˆ๋‹ค๋ฉด, ๋’ค๋กœ ํ›„์ง„ํ•  ๋•Œ์˜ ๋กœ์ง์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š”๊ฐ€ ์˜€๋„ค์š”.

๊ทธ๋ฆฌ๊ณ  ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ? ์ด๋Ÿฐ๊ฒƒ๋„ ๋ฐฉํ–ฅ์„ ์ •ํ•˜๋‹ˆ๊นŒ ์‚ด์ง ํ—ท๊ฐˆ๋ฆฌ ๋”๋ผ๊ตฌ์š”.

dfs bfs๋ฌธ์ œ๋Š” ํ•ญ์ƒ ์žฌ๋ฐŒ๋Š”๊ฒƒ ๊ฐ™๋„ค์š”. ์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

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

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

using namespace std;

int N, M, answer = 0;
vector<vector<int>> board;

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

void dfs(int y, int x, int d) {
    if (board[y][x] == PATH) {
        board[y][x] = VISITED;
        answer++;
    }

    for (int i = 0; i < 4; i++) {
        int d_ = (d + 3) % 4;
        int y_ = y + offset[d_][0];
        int x_ = x + offset[d_][1];

        if (0 <= y_ && y_ < N && 0 <= x_ && x_ < M && board[y_][x_] == PATH) {
            dfs(y_, x_, d_);
            return;
        }
        d = d_;
    }
    int backD = (d + 2) % 4;
    int backY = y + offset[backD][0];
    int backX = x + offset[backD][1];

    if (board[backY][backX] != WALL) dfs(backY, backX, d);
}

int main() {
    cin >> N >> M;
    
    int curY, curX, d;
    cin >> curY >> curX >> d;

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

    dfs(curY, curX, d);
    cout << answer << endl;

    return 0;
}

Comment on lines +32 to +34
public static void turnLeft() {
d = (d + 3) % 4;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด์ง€๋งŒ ํ•จ์ˆ˜๋ถ„๋ฆฌ๋ฅผ ์ž˜ํ•ด์„œ ๋ณด๊ธฐ ํŽธํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ์ข‹๋„ค์š”!

d = sc.nextInt();

map = new int[n][m];
cleaned = new boolean[n][m];
Copy link
Collaborator

Choose a reason for hiding this comment

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

๊ทผํ˜ธ๋‹˜ ์ฝ”๋“œ์—์„œ ๋ฐ”๋กœ ์ ์šฉ๊ฐ€๋Šฅํ•œ์ง€๋Š” ๋ชจ๋ฅด๊ฐฐ์ง€๋งŒ,
์ด๋ ‡๊ฒŒ ๋ถˆ๋ฆฌ์–ธ ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , map ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•ด์„œ ๋ฐฉ๋ฌธ ํ‘œ์‹œ๋ฅผ ํ•  ์ˆ˜ ๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์˜น ๋‹ค์Œ ๋น„์Šทํ•œ ์œ ํ˜• ํ’€์–ด๋ณผ ๋•Œ ์ ์šฉ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

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.

์ž…๋ ฅ์ด (r, c) ์ธ์ค„ ๋ชฐ๋ผ์„œ 1์‹œ๊ฐ„์„ ํ—ค๋งท๋„ค์š”.. ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ข€ ํ‹€์–ด์ ธ์„œ ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์—ฟ๋Š”๋ฐ๋„ ์ œ๊ฐ€ ์ด์ „์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ์™€ ๋งŽ์ด ๋‹ค๋ฅด๋„ค์š”.

์ฝ”๋“œ ์ž์ฒด๋Š” ๊ฐ•๋ž˜๋‹˜ ์ฒ˜๋Ÿผ dfs๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค..(์‚ฌ์‹ค ์ค‘๊ฐ„์— dfs๊ฐ€ ์ž˜ ์•ˆ๋˜์„œ ์ฐธ๊ณ ๋ฅผ ์ข€ ํ–ˆ์Šต๋‹ˆ๋‹ค^^;) ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ณ  ๋ถˆํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ๊ฐ€ ๋‹ค๋ถ„ํ•œ๊ฑฐ ๊ฐ™์•„ ๋‹ค์Œ๋ถ€ํ„ด ๋” ์ž˜ ํ’€์–ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

code
import java.io.*;
import java.util.*;

public class Sol14503 {

    static int answer = 0;
    static int n;
    static int m;
    static Pos cur;
    static int dir; // 0: ์ƒ, 1: ์šฐ, 2: ํ•˜, 3: ์ขŒ
    static int[] dx = {0, 1, 0, -1};
    static int[] dy = {-1, 0, 1, 0};
    static int[][] grid;

    public static void main(String[] args) throws IOException {
        input();
        dfs();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(answer));
        bw.close();
    }

    private static void dfs() {
        if (grid[cur.y][cur.x] == 0) {
            cur.clean();
            answer++;
        }
        for (int t = 0; t < 4; t++) {
            dir = (dir + 3) % 4;
            if (cur.canMove(dir)) {
                cur.move(dir);
                dfs();
                return;
            }
        }
        if (cur.backward(dir)) {
            dfs();
        }
    }

    private static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        int r = Integer.parseInt(st.nextToken());
        int c = Integer.parseInt(st.nextToken());
        cur = new Pos(c, r);
        dir = Integer.parseInt(st.nextToken());

        grid = new int[n][m];

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < m; j++) {
                grid[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        br.close();
    }

    private static class Pos {
        public int x;
        public int y;

        Pos(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public boolean canMove(int dir) {
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            return 0 <= nx && nx < m && 0 <= ny && ny < n && grid[ny][nx] == 0;
        }

        public void move(int dir) {
            x += dx[dir];
            y += dy[dir];
        }

        public boolean backward(int dir) {
            dir = (dir + 2) % 4;
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            if(0 <= nx && nx < m && 0 <= ny && ny < n && grid[ny][nx] != 1) {
                move(dir);
                return true;
            }
            return false;
        }

        public void clean() {
            grid[y][x] = 2;
        }
    }

}

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋‹ˆ ์ „์— ์ œ๊ฐ€ PRํ–ˆ๋˜ ๋ฌธ์ œ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•œ ๊ตฌํ˜„๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

ํŠน ๊ธฐ๋Šฅ์„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•ด์ค€๊ฒƒ์€ ๋Œ€ํ‘œ์ ์ธ ๊ตฌํ˜„๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
๋กœ์ง์„ ๋ฌธ์ œ์—์„œ ์•Œ๋ ค์ฃผ๊ฒŒ๋•Œ๋ฌธ์— ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•ด๋‚˜๊ฐ€๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๊ณ ๋ฏผํ–ˆ๋˜ ๋ถ€๋ถ„๋“ค์€ ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ ํšŒ์ „์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊ฒƒ์ธ๊ฐ€?
4๊ฐ€์ง€ ๋ฐฉํ–ฅ์„ ๋Œ๊ณ ๋‚œ ๋’ค ์ฒญ์†Œํ•  ๋ฐฉ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊ฒƒ์ธ๊ฐ€? ์˜€์Šต๋‹ˆ๋‹ค.
Found๋ผ๋Š” ๋ถˆ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด 4๊ฐ€์ง€๋ฐฉ์„ ๋Œ๊ณ ๋‚œ ๋’ค found๊ฐ€ false๋ผ๋ฉด ๋”์ด์ƒ ์ „์ง„ํ•  ๊ณณ์ด ์—†๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  ํ›„์ง„๊ฐ€๋Šฅ์„ฑ์„ ํŒ๋‹จํ•˜๋Š” 3๋ฒˆ์งธ ๋กœ์ง์œผ๋กœ ๊ฐ€๊ฒŒ๋” ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

45๋ถ„์ •๋„ ๊ฑธ๋ฆฐ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค!

N, M = map(int, input().split())
r, c, d = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(N)]

def clean_room(n, m, r, c, d, grid):
    # ๋ถ, ๋™, ๋‚จ, ์„œ ๋ฐฉํ–ฅ
    dr = [-1, 0, 1, 0]
    dc = [0, 1, 0, -1]

    cleaned = 0

    while True:
        #ํ˜„์žฌ ์นธ ์ฒญ์†Œ
        if grid[r][c] == 0:
            grid[r][c] = 2  #์ฒญ์†Œ ์™„๋ฃŒ ํ‘œ์‹œ
            cleaned += 1

        #์ฃผ๋ณ€ 4์นธ ์ค‘ ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ์นธ ํ™•์ธ
        found = False  #์ฒญ์†Œํ•  ๋นˆ ์นธ์ด ์žˆ๋Š”์ง€ ์ฒดํฌ
        for i in range(4):
            nd = (d + 3) % 4 #๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „
            nr, nc = r + dr[nd], c + dc[nd]

            if 0 <= nr < n and 0 <= nc < m and grid[nr][nc] == 0:
                d = nd  #ํšŒ์ „
                r, c = nr, nc  #์ „์ง„
                found = True
                break  #์ „์ง„ํ–ˆ์œผ๋ฏ€๋กœ ์ฆ‰์‹œ ๋ฃจํ”„ ์ข…๋ฃŒ
            else:
                d = nd  #๋ฐฉํ–ฅ๋งŒ ํšŒ์ „

        #์ฒญ์†Œํ•  ์นธ์ด ์—†์œผ๋ฉด ํ›„์ง„ ์‹œ๋„
        if not found:
            br, bc = r - dr[d], c - dc[d]
            if 0 <= br < n and 0 <= bc < m and grid[br][bc] != 1:
                r, c = br, bc  #ํ›„์ง„
            else:
                break  #ํ›„์ง„ ๋ถˆ๊ฐ€ ์‹œ ์ข…๋ฃŒ

    return cleaned

print(clean_room(N, M, r, c, d, grid))

@kokeunho kokeunho merged commit 65798d6 into 17-kokeunho Feb 28, 2025
1 check passed
@g0rnn g0rnn deleted the 18-kokeunho branch March 1, 2025 13:54
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