๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Code Tree

[์ฝ”๋“œํŠธ๋ฆฌ] ์ •์œก๋ฉด์ฒด ํ•œ๋ฒˆ ๋” ๊ตด๋ฆฌ๊ธฐ - ์‹œ๋ฎฌ๋ ˆ์ด์…˜ (๊ธฐ์ถœ๋ฌธ์ œ)

728x90

์‚ผ์„ฑ SW ์—ญ๋Ÿ‰ํ…Œ์ŠคํŠธ 2021 ํ•˜๋ฐ˜๊ธฐ ๊ธฐ์ถœ

์ •์œก๋ฉด์ฒด ํ•œ๋ฒˆ ๋” ๊ตด๋ฆฌ๊ธฐ

1์ด์ƒ 6์ดํ•˜ ์ค‘ ์ž„์˜์˜ ์ˆซ์ž๊ฐ€ ๊ทธ๋ ค์ง„ n * n ๊ฒฉ์žํŒ์— ํ•œ ๋ฉด์ด 1 * 1 ํฌ๊ธฐ์ธ ์ •์œก๋ฉด์ฒด๋ฅผ ๋†“์—ฌ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒฉ์žํŒ์—์„œ ์ •์œก๋ฉด์ฒด๋ฅผ ๊ตด๋ฆฌ๋ คํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด 4 * 4 ํฌ๊ธฐ์˜ ๊ฒฉ์žํŒ์ด ์ฃผ์–ด์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ ์ •์œก๋ฉด์ฒด์˜ ๊ฐ ๋ฉด์—๋Š” 1๋ถ€ํ„ฐ 6๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ฐ์—ฌ์ ธ ์žˆ๊ณ  m๋ฒˆ์— ๊ฑธ์ณ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ณ„์† 1์นธ์”ฉ ๊ตด๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋งˆ์ฃผ๋ณด๋Š” ๋ฉด์— ์ ํ˜€์žˆ๋Š” ์ˆซ์ž์˜ ํ•ฉ์€ ์ •ํ™•ํžˆ 7์ž…๋‹ˆ๋‹ค.

์ฃผ์‚ฌ์œ„๋Š” ํ•ญ์ƒ ์ดˆ๊ธฐ์— ๊ฒฉ์žํŒ์˜ 1ํ–‰ 1์—ด์— ๋†“์—ฌ์ ธ ์žˆ๊ณ , ์ฒ˜์Œ์—๋Š” ํ•ญ์ƒ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์›€์ง์ž…๋‹ˆ๋‹ค.

 

(์ค‘๋žต)

 

n * n ํฌ๊ธฐ์˜ ๊ฒฉ์žํŒ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, m๋ฒˆ ์ง„ํ–‰ํ•˜๋ฉฐ ์–ป๊ฒŒ๋˜๋Š” ์ ์ˆ˜์˜ ์ด ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์„ธ์š”.

์ž…๋ ฅ ํ˜•์‹

์ฒซ์งธ์ค„์— ๊ฒฉ์ž ํฌ๊ธฐ n, ๊ทธ๋ฆฌ๊ณ  ๊ตด๋ฆฌ๋Š” ํšŸ์ˆ˜ m์ด ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘˜์งธ์ค„๋ถ€ํ„ฐ n+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๊ฒฉ์žํŒ์— ์ ํ˜€์žˆ๋Š” ์ˆ˜ n๊ฐœ๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

  • 2 ≤ n ≤ 20
  • 1 ≤ m ≤ 1000
  • 1 ≤ ๋งํŒ์— ์“ฐ์—ฌ์ง„ ์ˆ˜ ≤ 6

์ถœ๋ ฅ ํ˜•์‹

์ฃผ์‚ฌ์œ„๋ฅผ m๋ฒˆ ๊ตด๋ ธ์„ ๋•Œ ๊นŒ์ง€ ์–ป๊ฒŒ๋˜๋Š” ์ด ์ ์ˆ˜์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 


 

์ผ๋‹จ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๋Š” ๊ต‰์žฅํžˆ..๋‹จ์ˆœํ•œ ๋ฌธ์ œ์ด๋‹ค.

bfs ํ˜น์€ dfs๋กœ ํƒ์ƒ‰ํ•˜๊ณ  ์ฃผ์‚ฌ์œ„๋งŒ ์ž˜ ๊ตด๋ ค์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ.

๊ทธ๋Ÿฐ๋ฐ ์ฃผ์‚ฌ์œ„ ๋Œ๋ฆด๋•Œ ์ˆซ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ์ €์žฅํ•˜๊ณ  ๊ฐฑ์‹ ํ•ด์•ผํ• ์ง€ ๊ฐ์ด ์•ˆ์žกํ˜€์„œ ํ•ด์„ค์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.

 

์ค‘๊ฐ„์— ์‹ค์ˆ˜ ํ•˜๋‚˜ ๋•Œ๋ฌธ์— ๋ช‡์‹ญ๋ถ„ ๋‚ ๋ ค๋จน์—ˆ๋Š”๋ฐ,,

์ •์‹ ์ฐจ๋ฆฌ์ž!!!!

 

 

๋‚˜์˜ ํ’€์ด

#include <iostream>
#include <queue>

using namespace std;

int n;
int m;
int map[21][21];
pair<int,int> dice;
int moveDirection = 0; // 0=์˜ค๋ฅธ์ชฝ,1=์•„๋ž˜,2=์™ผ์ชฝ,3=์œ„
int Bottom = 6; // ์ฃผ์‚ฌ์œ„ ๋ฐ”๋‹ฅ์ˆซ์ž ์ดˆ๊ธฐ๊ฐ’
int Up = 1;
int Right = 3;
int Front = 2;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int Point = 0;

void checkDirection(int num){
    // cout << "Bottom = "<<Bottom<<"\n";
    if(Bottom > num) {
        if(moveDirection<3) moveDirection++;
        else moveDirection = 0;
    } else if(Bottom < num){
        if(moveDirection>0) moveDirection--;
        else moveDirection = 3;
    } else return;
}

void checkBFS(int x, int y){
    // ํ˜„์žฌ ์ขŒํ‘œ ๋ฐ›์•„์•ผํ•จ
    int visited[21][21]={0};
    queue<pair<int,int>> q;
    q.push({x,y});
    visited[x][y] = 1;
    int tmp = map[x][y];
    int cnt = 1;
    while(!q.empty()){
        pair<int,int> a = q.front();
        q.pop();
        for(int i=0; i<4; ++i){
            int nx = a.first + dx[i];
            int ny = a.second + dy[i];
            if(nx<0 || ny<0 || nx>n || ny>n) continue;
            if(map[nx][ny] != tmp) continue;
            if(visited[nx][ny]) continue;
            q.push({nx,ny});
            visited[nx][ny] = 1;
            cnt++;
        }
    }
    
    Point += tmp * cnt;
    // cout << tmp << " * " << cnt << " => "<< Point<<"\n";
    checkDirection(tmp);
}

void moveDice(){
    // cout << "\n"<<dice.first <<" "<< dice.second << "\n";
    int tmp = Up;
    if(moveDirection==0){ // ์˜ค๋ฅธ์ชฝ
        if(dice.second + 1 > n){
            moveDirection = 2;
            moveDice();
            return;
        }
        else {
            // cout << "right\n";
            Up = 7 - Right;
            Right = tmp;
            Bottom = 7 - Up;
            dice.second++;
        }
    }
    else if(moveDirection==1){ // ์•„๋ž˜
        if(dice.first + 1 > n){
            moveDirection = 3;
            moveDice();
            return;
        }else {
            // cout << "down\n";
            Up = 7-Front;
            Front = tmp;
            Bottom = 7-Up;
            dice.first ++;
        }
    }
    else if(moveDirection==2){ // ์™ผ์ชฝ
        if(dice.second - 1 < 1){
            moveDirection = 0;
            moveDice();
            return;
        }else {
            // cout << "left\n";
            Up = Right;
            Right = 7-tmp;
            Bottom = 7-Up;
            dice.second --;
        }
    }
    else if(moveDirection==3){ // ์œ„
        if(dice.first - 1 < 1){
            moveDirection = 1;
            moveDice();
            return;
        }else {
            // cout << "up\n";
            Up = Front;
            Front = 7-tmp;
            Bottom = 7-Up;
            dice.first --;
        }
    }
    
}

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

    cin>>n>>m;
    for(int i=1; i<=n; ++i){
        for(int j=1; j<=n; ++j){
            cin >> map[i][j];
        }
    }
    dice = {1,1};
    for(int i=0; i<m; ++i){
        moveDice();
        checkBFS(dice.first, dice.second);
    }
    cout << Point;
    return 0;
}

 

728x90