๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 21608๋ฒˆ - ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต

by dkswnkk 2022. 9. 18.

๋ฌธ์ œ

 

21608๋ฒˆ: ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต

์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต์—๋Š” ๊ต์‹ค์ด ํ•˜๋‚˜ ์žˆ๊ณ , ๊ต์‹ค์€ N×N ํฌ๊ธฐ์˜ ๊ฒฉ์ž๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ํ•™๊ต์— ๋‹ค๋‹ˆ๋Š” ํ•™์ƒ์˜ ์ˆ˜๋Š” N2๋ช…์ด๋‹ค. ์˜ค๋Š˜์€ ๋ชจ๋“  ํ•™์ƒ์˜ ์ž๋ฆฌ๋ฅผ ์ •ํ•˜๋Š” ๋‚ ์ด๋‹ค. ํ•™์ƒ์€ 1๋ฒˆ๋ถ€ํ„ฐ N2๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

int N, ans;
int map[21][21];
int favorite[442][5];
int dr[4] = {0,0,-1,1};
int dc[4] = {-1,1,0,0};
int peo[442][4];

vector<pair<int,int>> find_blank(){ // ํ˜„์žฌ ๋น„์–ด์žˆ๋Š” ์ขŒํ‘œ์ฐพ๊ธฐ
    vector<pair<int,int>> v; // ๋น„์–ด์žˆ๋Š” ์นธ์˜ ์ขŒํ‘œ๋ฅผ ๋‹ด์„ ๋ฒกํ„ฐ
    for(int i=0; i<N; i++){
        for(int k=0; k<N; k++){
            if(map[i][k] == 0) v.push_back({i, k});
        }
    }
    return v;
}

vector<pair<int,int>> find_near_blank(vector<pair<int,int>> v){
    vector<pair<int,int>> coor;
    vector<pair<int,pair<int,int>>> blank_cnt_check;
    int max_black_cnt = 0;
    for(auto it : v){
        int r = it.first;
        int c = it.second;
        int blank_cnt_temp = 0;
        for(int i=0; i<4; i++){
            int nr = r + dr[i];
            int nc = c + dc[i];
            if(nr >= 0 && nr < N && nc >= 0 && nc < N){
                for(int k = 1; k < 5; k++){
                    if(map[nr][nc] == 0) blank_cnt_temp++;
                }
            }
        }
        if(blank_cnt_temp >= max_black_cnt){
            max_black_cnt = blank_cnt_temp;
            blank_cnt_check.push_back({max_black_cnt,{r,c}});
        }
    }
    for(auto it: blank_cnt_check){
        if(it.first == max_black_cnt) coor.push_back({it.second.first, it.second.second});
    }
    return coor;
}

vector<pair<int,int>> find_near_favorite(int idx, vector<pair<int,int>> blank_coor){  // ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ๊ฐ€์žฅ ๋งŽ์€ ์ธ์ •ํ•œ ์นธ ์ขŒํ‘œ ์ฐพ๊ธฐ
    vector<pair<int,int>> coor;
    vector<pair<int,pair<int,int>>> favorite_cnt_check;
    int max_favorite_cnt = 0;
    for(auto it: blank_coor){
        int r = it.first;
        int c = it.second;
        int favorite_cnt_temp = 0;
        for(int i=0; i<4; i++){
            int nr = r + dr[i];
            int nc = c + dc[i];
            if(nr >= 0 && nr < N && nc >= 0 && nc < N){
                for(int k = 1; k < 5; k++){
                    if(favorite[idx][k] == map[nr][nc]) favorite_cnt_temp++;
                }
            }
        }
        if(favorite_cnt_temp >= max_favorite_cnt){
            max_favorite_cnt = favorite_cnt_temp;
            favorite_cnt_check.push_back({max_favorite_cnt, {r, c}});
        }
    }
    for(auto it: favorite_cnt_check){
        if(it.first == max_favorite_cnt) coor.push_back({it.second.first, it.second.second});
    }
    return coor;
}

vector<pair<int,int>> find_min_r(vector<pair<int,int>> coor){
    int min_r = 1e9;
    vector<pair<int,int>> v;
    for(auto it : coor) min_r = min(min_r, it.first);
    for(auto it : coor){
        if(it.first == min_r) v.push_back({it.first, it.second});
    }
    return v;
}

vector<pair<int,int>> find_min_c(vector<pair<int,int>> coor){
    int min_c = 1e9;
    vector<pair<int,int>> v;
    for(auto it : coor) min_c = min(min_c, it.second);
    for(auto it : coor){
        if(it.second == min_c) v.push_back({it.first, it.second});
    }
    return v;
}


void survey(){
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            int score = 0;
            for(int k=0; k<4; k++){
                int nr = i + dr[k];
                int nc = j + dc[k];
                if(nr >= 0 && nr < N && nc >= 0 && nc < N){
                    for(int z = 0; z<4; z++){
                        if(peo[map[i][j]][z] == map[nr][nc]) score++;
                    }
                }
            }
            if(score == 1) ans += 1;
            else if(score == 2) ans += 10;
            else if(score == 3) ans += 100;
            else if(score == 4) ans += 1000;
        }
    }
}

void simulation(){
    for(int i=0; i<N*N; i++){
        vector<pair<int,int>> coor = find_near_favorite(i, find_blank()); // ๋น„์–ด์žˆ๋Š” ์นธ ์ค‘์—์„œ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ์ธ์ ‘ํ•œ ์นธ์— ๊ฐ€์žฅ ๋งŽ์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.
        if(coor.size() > 1){ // 1์„ ๋งŒ์กฑํ•˜๋Š” ์นธ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด, ์ธ์ ‘ํ•œ ์นธ ์ค‘์—์„œ ๋น„์–ด์žˆ๋Š” ์นธ์ด ๊ฐ€์žฅ ๋งŽ์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.
            coor = find_near_blank(coor);
            if(coor.size() > 1){ // 2๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์นธ๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ํ–‰์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์œผ๋กœ ์ •ํ•œ๋‹ค.
                coor = find_min_r(coor);
                if(coor.size() > 1){ //  ๊ทธ๋Ÿฌํ•œ ์นธ๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด ์—ด์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.
                    coor = find_min_c(coor);
                }
            }
        }
        map[coor.front().first][coor.front().second] = favorite[i][0];
    }
    survey();
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>N;
    for(int i=0; i<N * N; i++){
        int _peo = 0;
        for(int k=0; k<5; k++){
            int inp; cin>>inp;
            if(k == 0) _peo = inp;
            if(k != 0) peo[_peo][k-1] = inp;
            favorite[i][k] = inp;
        }
    }
    
    simulation();
    
    cout<<ans;
}

 

ํ’€์ด

์ฝ”๋“œ๊ฐ€ ์ฑ„์  ํ˜„ํ™ฉ์˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ.. ์••๋„์ ์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ๊ธธ๊ธด ํ•œ๋ฐ ๊ทธ๋ž˜๋„ ํ•œ ๋ฒˆ์˜ ํ—ˆํŠผ์ง“๋„ ์—†์ด ์ถฉ์‹คํ•˜๊ฒŒ ํ•œ ๋ฒˆ์— ๊ตฌํ˜„์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ๋น„์–ด์žˆ๋Š” ์นธ ์ค‘์—์„œ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ์ธ์ ‘ํ•œ ์นธ์— ๊ฐ€์žฅ ๋งŽ์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.
  2. 1์„ ๋งŒ์กฑํ•˜๋Š” ์นธ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด, ์ธ์ ‘ํ•œ ์นธ ์ค‘์—์„œ ๋น„์–ด์žˆ๋Š” ์นธ์ด ๊ฐ€์žฅ ๋งŽ์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.
  3. 2๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์นธ๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ํ–‰์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์œผ๋กœ, ๊ทธ๋Ÿฌํ•œ ์นธ๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด ์—ด์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.
  • 1๋ฒˆ ์กฐ๊ฑด์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋จผ์ € ๋น„์–ด์žˆ๋Š” ์นธ์„ ์ฐพ๋Š” find_blank() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ์ธ์ ‘ํ•œ ์นธ์— ๊ฐ€์žฅ ๋งŽ์€ ์นธ์„ ์ฐพ๊ธฐ ์œ„ํ•ด find_near_favorite() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • 2๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ธฐ ์œ„ํ•ด์„œ find_near_blank() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • 3๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ธฐ ์œ„ํ•ด์„œ find_min_r() ๊ณผ find_min_c() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ํ–‰์˜ ๋ฒˆํ˜ธ์™€ ์—ด์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์„ ์ฐพ๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ survey() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ๋งŒ์กฑ๋„๋ฅผ ์กฐ์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€