[c++] BOJ 1941 :: μ†Œλ¬Έλ‚œ 칠곡주

문제

μ†Œλ¬Έλ‚œ 칠곡주 문제 λ°”λ‘œκ°€κΈ°

총 25λͺ…μ˜ μ—¬ν•™μƒλ“€λ‘œ 이루어진 μ—¬ν•™μƒλ°˜μ€ 5*5의 μ •μ‚¬κ°ν˜• 격자 ν˜•νƒœλ‘œ μžλ¦¬κ°€ λ°°μΉ˜λ˜μ—ˆκ³ , μ–Όλ§ˆ μ§€λ‚˜μ§€ μ•Šμ•„ μ΄λ‹€μ†œκ³Ό μž„λ„μ—°μ΄λΌλŠ” 두 학생이 두각을 λ‚˜νƒ€λ‚΄λ©° λ‹€λ₯Έ 학생듀을 νœ˜μ–΄μž‘κΈ° μ‹œμž‘ν–ˆλ‹€. 곧 λͺ¨λ“  여학생이 ‘μ΄λ‹€μ†œνŒŒ’와 ‘μž„λ„μ—°νŒŒ’의 두 파둜 κ°ˆλΌμ§€κ²Œ λ˜μ—ˆμœΌλ©°, μ–Όλ§ˆ μ§€λ‚˜μ§€ μ•Šμ•„ ‘μž„λ„μ—°νŒŒ’κ°€ μ„Έλ ₯을 ν™•μž₯μ‹œν‚€λ©° ‘μ΄λ‹€μ†œνŒŒ’λ₯Ό μœ„ν˜‘ν•˜κΈ° μ‹œμž‘ν–ˆλ‹€.

μœ„κΈ°μ˜μ‹μ„ λŠλ‚€ ‘μ΄λ‹€μ†œνŒŒ’의 학생듀은 과감히 ν˜„μž¬μ˜ 체제λ₯Ό ν¬κΈ°ν•˜κ³ , ‘μ†Œλ¬Έλ‚œ 칠곡주’λ₯Ό κ²°μ„±ν•˜λŠ” 것이 μœ μΌν•œ 생쑴 μˆ˜λ‹¨μž„μ„ κΉ¨λ‹¬μ•˜λ‹€. ‘μ†Œλ¬Έλ‚œ 칠곡주’λŠ” λ‹€μŒκ³Ό 같은 κ·œμΉ™μ„ λ§Œμ‘±ν•΄μ•Ό ν•œλ‹€.

  1. 이름이 이름인 만큼, 7λͺ…μ˜ μ—¬ν•™μƒλ“€λ‘œ κ΅¬μ„±λ˜μ–΄μ•Ό ν•œλ‹€.
  2. κ°•ν•œ 결속λ ₯을 μœ„ν•΄, 7λͺ…μ˜ μžλ¦¬λŠ” μ„œλ‘œ κ°€λ‘œλ‚˜ μ„Έλ‘œλ‘œ λ°˜λ“œμ‹œ 인접해 μžˆμ–΄μ•Ό ν•œλ‹€.
  3. ν™”ν•©κ³Ό λ²ˆμ˜μ„ μœ„ν•΄, λ°˜λ“œμ‹œ ‘μ΄λ‹€μ†œνŒŒ’의 ν•™μƒλ“€λ‘œλ§Œ ꡬ성될 ν•„μš”λŠ” μ—†λ‹€.
  4. κ·ΈλŸ¬λ‚˜ 생쑴을 μœ„ν•΄, ‘μ΄λ‹€μ†œνŒŒ’κ°€ λ°˜λ“œμ‹œ μš°μœ„λ₯Ό 점해야 ν•œλ‹€. λ”°λΌμ„œ 7λͺ…μ˜ 학생 쀑 ‘μ΄λ‹€μ†œνŒŒ’의 학생이 적어도 4λͺ… 이상은 λ°˜λ“œμ‹œ ν¬ν•¨λ˜μ–΄ μžˆμ–΄μ•Ό ν•œλ‹€.

μ—¬ν•™μƒλ°˜μ˜ 자리 λ°°μΉ˜λ„κ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, ‘μ†Œλ¬Έλ‚œ 칠곡주’λ₯Ό κ²°μ„±ν•  수 μžˆλŠ” λͺ¨λ“  경우의 수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

 

풀이

  1. dfs둜 κ΅¬ν•˜κΈ° => 쀑간에 T자 ν˜•νƒœ ꡬ할 수 μ—†μŒ
  2. next_permutation으둜 κ΅¬ν•˜κΈ° (brute force)

 

μ½”λ“œ

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

#define PRINCESS 7

using namespace std;

int result = 0;
char position[5][5];
int direction[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
vector<int> permuVector(25);

bool isOverFour(vector<pair<int, int>>& posArr) {
    int exitNum = 0;
    for (int i = 0; i < posArr.size(); i++) {
        if (position[posArr[i].first][posArr[i].second] == 'Y') exitNum++;
        if (exitNum > 3) return false; // λ°±νŠΈλž˜ν‚Ή
    }
    return true;
}

bool isAdjacent(vector<pair<int, int>>& posArr) {
    // bfs
    int map[5][5];
    int visited[5][5];

    fill(&map[0][0], &map[4][5], 0);
    fill(&visited[0][0], &visited[4][5], 0);

    for (int i = 0; i < posArr.size(); i++) {
        map[posArr[i].first][posArr[i].second] = 1;
    }

    queue<pair<int, int>> que;
    que.push(posArr[0]);
    visited[posArr[0].first][posArr[0].second] = 1;

    int count = 1;

    while (!que.empty()) {
        auto pos = que.front(); que.pop();

        for (int i = 0; i < 4; i++) {
            int row = pos.first + direction[i][0];
            int col = pos.second + direction[i][1];

            if (row < 0 || col < 0 || row>4 || col >4) continue;

            if (map[row][col] != 1 || visited[row][col] == 1) continue;

            visited[row][col] = 1;
            count++;
            que.push(make_pair(row, col));

            if (count == 7)
                return true;
        }
    }

    return false;
}

vector<pair<int, int>> getPositionByIndex(vector<int> permu) {
    vector<pair<int, int>> posArr;

    for (int i = 0; i < permu.size(); i++) {
        if (permu[i] == 1) {
            posArr.push_back(make_pair(i / 5, i % 5));
        }
    }

    return posArr;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    for (int i = 0; i < 5; i++) {
        string str; cin >> str;
        for (int j = 0; j < str.length(); j++) {
            position[i][j] = str[j];
        }
    }

    int num = 0;
    for (int i = permuVector.size() - 1; ; i--, num++) {
        if (num == PRINCESS) break;
        permuVector[i] = 1;
    }

    int result = 0;
    do {
        vector<pair<int, int >> posArr = getPositionByIndex(permuVector);
        if (isOverFour(posArr) && isAdjacent(posArr)) result++;
    } while (next_permutation(permuVector.begin(), permuVector.end()));


    cout << result;
}

 

 

κ²°κ³Ό

아이디 λ©”λͺ¨λ¦¬ μ‹œκ°„
gmldms784 2028 172
gmldms784 2028 248

μœ„κ°€ μž…μΆœλ ₯ μ΅œμ ν™”ν•œ μ½”λ“œ

λ°˜μ‘ν˜•