[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ธ”๋ก ์ด๋™ํ•˜๊ธฐ :: c++, ์ฝ”๋“œ, DFS ์“ฐ๋ฉด ์•ˆ๋˜๋Š” ์ด์œ , BFS

๋ธ”๋ก ์ด๋™ํ•˜๊ธฐ

โ˜…โ˜…โ˜…โ˜†โ˜† LEVEL 3

๋ฌธ์ œ

๋ธ”๋ก ์ด๋™ํ•˜๊ธฐ ๋ฌธ์ œ ๋งํฌ

๋ฌธ์ œ ์„ค๋ช…

๋กœ๋ด‡๊ฐœ๋ฐœ์ž ๋ฌด์ง€๋Š” ํ•œ ๋‹ฌ ์•ž์œผ๋กœ ๋‹ค๊ฐ€์˜จ ์นด์นด์˜ค๋ฐฐ ๋กœ๋ด‡๊ฒฝ์ง„๋Œ€ํšŒ์— ์ถœํ’ˆํ•  ๋กœ๋ด‡์„ ์ค€๋น„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ค€๋น„ ์ค‘์ธ ๋กœ๋ด‡์€ 2 x 1 ํฌ๊ธฐ์˜ ๋กœ๋ด‡์œผ๋กœ ๋ฌด์ง€๋Š” 0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ N x N ํฌ๊ธฐ์˜ ์ง€๋„์—์„œ 2 x 1 ํฌ๊ธฐ์ธ ๋กœ๋ด‡์„ ์›€์ง์—ฌ (N, N) ์œ„์น˜๊นŒ์ง€ ์ด๋™ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋กœ๋ด‡์ด ์ด๋™ํ•˜๋Š” ์ง€๋„๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ, ์ƒ๋‹จ์˜ ์ขŒํ‘œ๋ฅผ (1, 1)๋กœ ํ•˜๋ฉฐ ์ง€๋„ ๋‚ด์— ํ‘œ์‹œ๋œ ์ˆซ์ž 0์€ ๋นˆ์นธ์„ 1์€ ๋ฒฝ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋กœ๋ด‡์€ ๋ฒฝ์ด ์žˆ๋Š” ์นธ ๋˜๋Š” ์ง€๋„ ๋ฐ–์œผ๋กœ๋Š” ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋กœ๋ด‡์€ ์ฒ˜์Œ์— ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ขŒํ‘œ (1, 1) ์œ„์น˜์—์„œ ๊ฐ€๋กœ๋ฐฉํ–ฅ์œผ๋กœ ๋†“์—ฌ์žˆ๋Š” ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ, ์•ž๋’ค ๊ตฌ๋ถ„์—†์ด ์›€์ง์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

แ„‡แ…ณแ†ฏแ„…แ…ฅแ†จแ„‹แ…ตแ„ƒแ…ฉแ†ผ-1.jpg

๋กœ๋ด‡์ด ์›€์ง์ผ ๋•Œ๋Š” ํ˜„์žฌ ๋†“์—ฌ์žˆ๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„ ๊ทธ๋ฆผ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์ด๋™ํ•œ๋‹ค๋ฉด (1, 2), (1, 3) ๋‘ ์นธ์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜๋ฉฐ, ์•„๋ž˜๋กœ ์ด๋™ํ•œ๋‹ค๋ฉด (2, 1), (2, 2) ๋‘ ์นธ์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋กœ๋ด‡์ด ์ฐจ์ง€ํ•˜๋Š” ๋‘ ์นธ ์ค‘ ์–ด๋Š ํ•œ ์นธ์ด๋ผ๋„ (N, N) ์œ„์น˜์— ๋„์ฐฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋กœ๋ด‡์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์กฐ๊ฑด์— ๋”ฐ๋ผ ํšŒ์ „์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

แ„‡แ…ณแ†ฏแ„…แ…ฅแ†จแ„‹แ…ตแ„ƒแ…ฉแ†ผ-2.jpg

์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋กœ๋ด‡์€ 90๋„์”ฉ ํšŒ์ „ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ, ๋กœ๋ด‡์ด ์ฐจ์ง€ํ•˜๋Š” ๋‘ ์นธ ์ค‘, ์–ด๋Š ์นธ์ด๋“  ์ถ•์ด ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, ํšŒ์ „ํ•˜๋Š” ๋ฐฉํ–ฅ(์ถ•์ด ๋˜๋Š” ์นธ์œผ๋กœ๋ถ€ํ„ฐ ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์— ์žˆ๋Š” ์นธ)์—๋Š” ๋ฒฝ์ด ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋กœ๋ด‡์ด ํ•œ ์นธ ์ด๋™ํ•˜๊ฑฐ๋‚˜ 90๋„ ํšŒ์ „ํ•˜๋Š” ๋ฐ๋Š” ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ ์ •ํ™•ํžˆ 1์ดˆ ์ž…๋‹ˆ๋‹ค.

0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ์ง€๋„์ธ board๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋กœ๋ด‡์ด (N, N) ์œ„์น˜๊นŒ์ง€ ์ด๋™ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • board์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด๋Š” 5 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • board์˜ ์›์†Œ๋Š” 0 ๋˜๋Š” 1์ž…๋‹ˆ๋‹ค.
  • ๋กœ๋ด‡์ด ์ฒ˜์Œ์— ๋†“์—ฌ ์žˆ๋Š” ์นธ (1, 1), (1, 2)๋Š” ํ•ญ์ƒ 0์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • ๋กœ๋ด‡์ด ํ•ญ์ƒ ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

์ฝ”๋“œ

#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int dirArray[12][4] = { { 0, 1, 0, 1 },{ 1, 0, 1, 0 },{ 0, -1, 0, -1 },{ -1, 0, -1, 0 },{ 1, 1, 0, 0 },{ 0, 0, -1, -1 },{ 0, 0, 1, -1 },{ -1, 1, 0, 0 },{ 1, 1, 0, 0 },{ 0, 0, -1, -1 },{ 1, -1, 0, 0 },{ 0, 0, -1, 1 } };
// ์•„ => ์™ผ => ์œ„ => ์˜ค => ๊ฐ€๋กœํ˜• => ์„ธ๋กœํ˜•
int row;
vector<vector<int>> board;

struct Robot { // ๊ณ„์‚ฐ ํŽธํ•˜๊ฒŒ struct ์ •์˜
    int x; int y; int x2; int y2; int time;
    Robot(int a, int b, int c, int d, int e) {
        x = a; y = b; x2 = c; y2 = d; time = e;
    }
};

bool isThereOkay(int r1, int c1, int r2, int c2) {
    // ๋ฒ”์œ„ ๋ฐ ๋ฒฝ ์ฒดํฌ
    if (r1<0 || c1<0 || r2<0 || c2<0)
        return false;
    if (r1 > row || r2 > row || c1 > row || c2 > row)
        return false;
    if (board[r1][c1] == 1 || board[r2][c2] == 1)
        return false;
    return true;
}

vector<int> whereCanIGo(Robot& pos) {
    vector<int> candidate;

    for (int i = 0; i<4; i++) {
        pos.x += dirArray[i][0];
        pos.y += dirArray[i][1];
        pos.x2 += dirArray[i][0];
        pos.y2 += dirArray[i][1];
        if (isThereOkay(pos.x, pos.y, pos.x2, pos.y2)) {
            candidate.push_back(i);
            if (pos.x == pos.x2) { // ๊ฐ€๋กœ ๋ฐฐ์น˜์ผ ๋•Œ
                if (i == 1) {
                    // ์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•˜๋ฉด 4, 6๋ฒˆ์˜ ํšŒ์ „๋„ ๊ฐ€๋Šฅ
                    candidate.push_back(4);
                    candidate.push_back(6);
                }
                if (i == 3) {
                    // ์œ„์ชฝ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•˜๋ฉด 5, 7๋ฒˆ์˜ ํšŒ์ „๋„ ๊ฐ€๋Šฅ
                    candidate.push_back(5);
                    candidate.push_back(7);
                }
            }
            else if (pos.y== pos.y2) { // ์„ธ๋กœ ๋ฐฐ์น˜
                if (i == 0) {
                    candidate.push_back(8);
                    candidate.push_back(11);
                }
                if (i == 2) {
                    candidate.push_back(9);
                    candidate.push_back(10);
                }
            }
        }
        pos.x -= dirArray[i][0];
        pos.y -= dirArray[i][1];
        pos.x2 -= dirArray[i][0];
        pos.y2 -= dirArray[i][1];
    }
    return candidate;
}


string makeKey(int ax, int ay, int bx, int by) {
    if (ax > bx || (ax == bx && ay > by)) {
        int tmp = ax;
        int tmp2 = ay;
        ax = bx; ay = by;
        bx = tmp; by = tmp2;
    }
    return to_string(ax) + "/" + to_string(ay) + "//" + to_string(bx) + "/" + to_string(by);
}

int solution(vector<vector<int>> board2) {
    int answer = 1e8;
    board = board2; // ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ
    row = board.size() - 1; // ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ

    vector<string> visited;

    queue<Robot> q;
    Robot* first = new Robot(0, 0, 0, 1, 0);
    q.push(*first); // ์ฒซ ์œ„์น˜ ์ €์žฅ
    visited.push_back(makeKey(0, 0, 0, 1)); // ์ฒซ ์œ„์น˜ ๋ฐฉ๋ฌธ ๊ธฐ๋ก

    while (q.size()) {
        auto a = q.front(); q.pop(); // queue์— ๋‹ด๊ธด ์œ„์น˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ

        if ((a.x == row && a.y == row) || (a.x2 == row && a.y2 == row)) { // ๋„์ฐฉํ–ˆ์„ ๋•Œ
            answer = a.time;
            break;
        }

        vector<int> candidate = whereCanIGo(a); // ํ˜„์žฌ ์œ„์น˜์—์„œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ์˜ ์ •๋ณด ๋ฐ›์•„์˜ค๊ธฐ

        while(candidate.size()){
            int idx = candidate.back(); candidate.pop_back();

            int r1 = a.x + dirArray[idx][0];
            int c1 = a.y + dirArray[idx][1];
            int r2 = a.x2 + dirArray[idx][2];
            int c2 = a.y2 + dirArray[idx][3];

            if (r1 > r2 || (r1 == r2 && c1 > c2)) { // r1, c1์ด ์ž‘๊ฒŒ ์œ ์ง€
                int tmp = r1; int tmp2 = c1;
                r1 = r2; c1 = c2;
                r2 = tmp; c2 = tmp2;
            }

            if (find(visited.begin(), visited.end(), makeKey(r1, c1, r2, c2)) != visited.end())
                // ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์ด๋ฉด ๋›ฐ์–ด๋„˜๊ธฐ
                continue;

            visited.push_back(makeKey(r1, c1, r2, c2)); // ์œ„์น˜ ๋ฐฉ๋ฌธ ๊ธฐ๋ก
            Robot* tmp = new Robot(r1, c1, r2, c2, a.time + 1);
            q.push(*tmp); // ๋‹ค์Œ ์œ„์น˜ ์ €์žฅ
        }
    }


    return answer;
}

ํ’€์ด

์ด์ „ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋งฅ๋ฝ์œผ๋กœ ์ฝ์–ด์„œ, ๊ฐ ์œ„์น˜์—์„œ ๋งˆ์ง€๋ง‰ ๋„์ฐฉ ๊ตฌ๊ฐ„๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ์†Œ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•ด๋‘๊ณ  ์ด์šฉํ•˜๋Š” DP ๋ฌธ์ œ๋กœ ํ’€๋ ค๊ณ  ํ–ˆ๋‹ค. ์ž…๋ ฅ๊ฐ’๋„ 10^2 ์ดํ•˜๋กœ ๊ทธ๋‹ค์ง€ ํฌ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ n^4๊นŒ์ง€๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค. DFS๋กœ ์žฌ๊ท€๋ฅผ ๋Œ๋ฉด์„œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์ด๋ฉด ์ €์žฅํ•ด๋‘” ์ •๋ณด๋ฅผ ์“ฐ๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ DFS๋กœ ํ’€๋ฉด ๊ณ„์† ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 6๋ฒˆ๋ถ€ํ„ฐ ํ‹€๋ ธ๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค. ๊ทธ๋ž˜์„œ ์กฐ๊ฑด์„ ๋˜‘๊ฐ™์ด ํ•˜๊ณ  BFS๋กœ ๋ฐ”๊พธ์–ด ํ’€์—ˆ๋”๋‹ˆ ํ†ต๊ณผ์˜€๋‹ค.

ํ’€์ด ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • queue์— ๋‹ค์Œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋“ค์„ ๋„ฃ๋Š”๋‹ค.

    • ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋Š” whereCanIGo ํ•จ์ˆ˜๊ฐ€ ํŒ๋‹จํ•˜์—ฌ ๋„˜๊ฒจ์ค€๋‹ค.

    • ๋ฏธ๋ฆฌ ์ •์˜ํ•ด๋‘” direction ๋ฐฐ์—ด์„ ์ด์šฉํ•œ๋‹ค.

    • direction์—๋Š” ๋”ํ•ด์ฃผ์–ด์•ผํ•  ์ˆซ์ž๋“ค์ด ๋“ค์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ฐ index๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  •  
    • ์ค‘๊ฐ„์— ๊ฐ€๋กœ ๋ฐฐ์น˜์ผ ๋•Œ, ์„ธ๋กœ ๋ฐฐ์น˜์ผ ๋•Œ๋ฅผ ๋‚˜๋ˆ„์–ด๋‘” ๊ฒƒ์€ ํšŒ์ „์„ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.

      • ์˜ˆ๋ฅผ ๋“ค์–ด, 1๋ฒˆ์˜ ์ด๋™(์•„๋ž˜๋กœ์˜ ์ด๋™)์ด ๊ฐ€๋Šฅํ•˜๋ฉด ์•„๋ž˜ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ํšŒ์ „๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

      • ๋”ฐ๋ผ์„œ, ๊ฐ€๋กœ๋ฐฉํ–ฅ์ผ ๋•Œ i๊ฐ€ 1์ด๋ฉด 4์™€ 6๋„ ํ•จ๊ป˜ ํ›„๋ณด์— ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

  • ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ฐ˜์˜ํ•˜๋ฉด์„œ ๋ฐฉ๋ฌธ ๋ฐ ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•œ๋‹ค.

    • ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋Š” visited ๋ฐฐ์—ด์— string์˜ ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜์˜€๋‹ค.
    • string์€ makeKey ํ•จ์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด ์ฃผ๋ฉฐ, ์ฃผ์–ด์ง„ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด์„œ key๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
    • ํ•ด๋‹น ํ‚ค๋Š” ์œ„์น˜์— ๋Œ€ํ•ด uniqueํ•˜๋ฉฐ ์ด๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์ „ ์ •๋ ฌ์„ ํ•ด์ค€๋‹ค.
  • ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ๋„๋‹ฌํ•˜์˜€๋‹ค๋ฉด ๋‹ค์Œ ๊ฒฝ๋กœ๋ฅผ ์‚ดํŽด๋ณด๊ธฐ๋ฅผ ๊ทธ๋งŒ๋‘๊ณ  ๋‹ต์„ ๋‚ธ๋‹ค.

์ตœ๋‹จ๊ฒฝ๋กœ ๋ฌธ์ œ์—์„œ DFS๋ณด๋‹ค BFS๋ฅผ ์“ฐ๋Š” ์ด์œ 

DFS ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.


#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int row = 0; int col = 0;
vector<vector<int>> board;
map<string, int> memo;

int direction[12][4] = { {1, 0, 1, 0}, {0, -1, 0, -1}, {-1, 0, -1, 0}, {0, 1, 0, 1}, {1, 1, 0, 0}, {0, 0, -1, -1}, {0, 0, 1, -1}, {-1, 1, 0, 0},{ 1, 1, 0, 0 },{ 0, 0, -1, -1 },{ 1, -1, 0, 0 },{ 0, 0, -1, 1 } };
// ์•„ => ์™ผ => ์œ„ => ์˜ค => ๊ฐ€๋กœํ˜• => ์„ธ๋กœํ˜•


bool compare(pair<int, int> p1, pair<int, int> p2) {
    // ... ์œ„์™€ ๊ฐ™์Œ
}
bool isThereOkay(vector<pair<int, int>> pos) {
    // ... ์œ„์™€ ๊ฐ™์Œ
}

vector<int> whereCanIGo(vector<pair<int, int>>& pos) {
    // ... ์œ„์™€ ๊ฐ™์Œ
}

string makeKey(int ax, int ay, int bx, int by) {
    // ... ์œ„์™€ ๊ฐ™์Œ
}

int leastMove(vector<pair<int, int>>& pos) {
    if ((pos[0].first == row && pos[0].second == col) | (pos[1].first == row && pos[1].second == col))
        return 0;

    vector<int> candidate = whereCanIGo(pos);
    int max = 1e8;

    if (candidate.size() == 0)
        return 1e8;

    while (candidate.size()) {
        int idx = candidate.back(); candidate.pop_back();
        int tmp_ans = 0;

        vector<pair<int, int>> next_pos;
        next_pos.push_back(make_pair(pos[0].first + direction[idx][0], pos[0].second + direction[idx][1]));
        next_pos.push_back(make_pair(pos[1].first + direction[idx][2], pos[1].second + direction[idx][3]));
        sort(next_pos.begin(), next_pos.end(), compare);
        string key_str = makeKey(next_pos[0].first, next_pos[0].second, next_pos[1].first, next_pos[1].second);
        if (memo.find(key_str)!=memo.end())
            tmp_ans += memo[key_str];
        else {
            memo[key_str] = 1e8; // ๊ฐ™์€ ์œ„์น˜ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ
            int ret = leastMove(next_pos);
            memo[key_str] = ret; tmp_ans += ret;
        }

        if (max>tmp_ans)
            max = tmp_ans;
    }
    return max+1;
}

int solution(vector<vector<int>> board2) {
    int answer = 0;
    row = board2.size()-1;
    col = board2[0].size()-1;
    board = board2;

    vector<pair<int, int>> pos;
    pos.push_back(make_pair(0, 0));
    pos.push_back(make_pair(0, 1));
    memo[makeKey(0, 0, 0, 1)] = 1e8;

    answer = leastMove(pos);


    return answer;
}

๋‹ค๋ฅธ ๋ถ€๋ถ„์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ์žฌ๊ท€๋กœ ๋Œ๋ ค์„œ DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์ด๋‹ค. DFS๋Š” ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ ํ›„ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ ๊ฐ™์€ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฉ”๋ชจ์ด์ œ์ด์…˜์— ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋„ฃ๋Š” ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค.

 

https://www.acmicpc.net/board/view/27666

์œ„์˜ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•ด๋ณด๋ฉด, BFS์™€ DFS ๋ชจ๋‘ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์ง€๋งŒ BFS๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์ž๋งˆ์ž ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ์—์„œ๋Š” ์ฃผ๋กœ BFS๋ฅผ ์ด์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ BFS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ ํ•ฉํ•˜๋‹ค.

 

https://allg.tistory.com/30

์œ„์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ์กฐํ•ด๋ณด๋ฉด, DFS๋Š” ์ตœ์ ์˜ ๊ฒฝ๋กœ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ์— ํƒ์ƒ‰์„ ๋๋‚ด๋ฒ„๋ ค์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋˜๋‹ค๋ฅธ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค. BFS๋Š” ํ•ด์— ๋„๋‹ฌํ•˜์˜€๋”๋ผ๋„ ์ข…๋ฃŒํ•˜์ง€ ์•Š๊ณ , ๊ณ„์†ํ•ด์„œ ์ง„ํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

img

์˜ˆ๋ฅผ ๋“ค์–ด, ์ด์™€ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„ ์˜ˆ์ œ์—์„œ A => B์˜ ๊ฒฝ๋กœ๋Š” ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค. ๋ณธ์ธ์˜ ์ฝ”๋“œ์—์„œ๋„ ์ด๋ฏธ ๋“ค๋ฅธ ์ง€์ ์€ ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋˜๋Š” ๋งˆ์ง€๋ง‰ ์ง€์ ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋กœ ํ‘œ์‹œํ•ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค.

 

์ฑ„์  ๊ฒฐ๊ณผ

์‹œ๊ฐ„์€ ๊ทธ๋‹ค์ง€ ์ž˜ ๋‚˜์˜จ ๊ฒƒ ๊ฐ™์ง€ ์•Š๋‹ค.

 

์ฃผ์š” ์‚ฌํ•ญ

  • ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•œ ์กฐ๊ฑด์ด ์ฃผ์–ด์งˆ ๋•Œ๋Š” struct๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ„ํŽธํ•˜๋‹ค.
  • ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์—์„œ๋Š” BFS๊ฐ€ ์œ ์šฉํ•˜๋‹ค.

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

  • ๋ฐฉํ–ฅ์„ ๋ณ€์ˆ˜๋กœ ์žก์•„์„œ visited๋ฅผ ๊ตฌํ˜„ํ–ˆ๋˜๋ฐ, ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•œ ์ขŒํ‘œ์—์„œ ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์ธ ๊ฒƒ๊ณผ ๋ฐ˜๋Œ€ ์ขŒํ‘œ์—์„œ ์™ผ์ชฝ ๋ฐฉํ–ฅ์ธ ๊ฒƒ (๊ฒฐ๊ตญ ๋˜‘๊ฐ™์€ ๊ฒƒ)์„ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•˜๋Š” ์ง€ ์˜๋ฌธ์ด๋‹ค. ๊ทธ๋ƒฅ ๋ฌด์‹œํ•˜๊ณ  ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฒ€์‚ฌํ•  ์ƒ๊ฐํ•˜๊ณ  ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ํ•˜๋Š” ๊ฒƒ์ธ๊ฐ€?
๋ฐ˜์‘ํ˜•