Devlog

처음 시도한 풀이: 실패

#include <iostream>
#define MAX 10
using namespace std;
int main(){
    int R, C;
    cin >> R >> C;
    char map[MAX][MAX]={0};
    char update_map[MAX][MAX]={0};
    for(int i=1; i<R+1; i++){
        for(int j=1; j<C+1; j++){
            cin >> map[i][j];
        }
    }
    int count = 0;
    for(int i=1; i<R+1; i++){
        for(int j=1; j<C+1; j++){
            if(map[i][j]== 'X'){
                if((map[i-1][j]=='.' || map[i-1][j]== 0) && (map[i+1][j]=='.' || map[i+1][j]== 0) && (map[i][j-1]=='.' || map[i][j-1]== 0)){
                    update_map[i][j]='.';
                }
                else if((map[i-1][j]=='.' || map[i-1][j]== 0) && (map[i+1][j]=='.' || map[i+1][j]== 0) && (map[i][j+1]=='.' || map[i][j+1]== 0)){
                    update_map[i][j]='.';
                }
                else if((map[i][j-1]=='.' || map[i][j-1]== 0) && (map[i][j+1]=='.' || map[i][j+1]== 0) && (map[i-1][j]=='.'|| map[i-1][j]== 0)){
                    update_map[i][j]='.';
                }
                else if((map[i][j-1]=='.' || map[i][j-1]== 0) && (map[i][j+1]=='.' || map[i][j+1]== 0) && (map[i+1][j]=='.' || map[i+1][j]== 0)){
                    update_map[i][j]='.';
                }
                else{
                }
            }
        }
    }
    for(int i=1; i<=R+1; i++){
        for(int j=1; j<=C+1; j++){
            cout << update_map[i][j];
        }
    }
    return 0;
 }

처음에는

왼, 오, 위, 아래 중 3부분 이상이 바다이면 섬이 없어지기 때문에 해당 좌표가 섬이고 3가지 조건에 해당될 때 바다가 되는 것으로 구현

조건이 너무 길어지고 

해당 지도의 바깥을 감싸는 부분이 바다가 있다라는 전제를 체크하기 힘들었음

 

 

 

최종 코드

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int R, C;
    int count;
    int minX = 9999, minY = 9999, maxX= -1, maxY = -1;

    char map[12][12]={}, update_map[12][12]={};

    cin >> R >> C;

    for(int i=1; i<=R; i++){
        for(int j=1; j<=C; j++){
            cin >> map[i][j];
            update_map[i][j]='.';

        }
    }

    for(int i=1; i<=R; i++){
        for(int j=1; j<=C; j++){
            count = 0;
            if(map[i][j]== 'X'){
                if(map[i][j-1]=='X')
                    count++;
                if(map[i][j+1]=='X')
                    count++;
                if(map[i-1][j]=='X')
                    count++;
                if(map[i+1][j]=='X')
                    count++;        
                if(count == 2 || count == 3 || count == 4){
                    update_map[i][j]='X';
                    minX=min(i, minX);
                    minY=min(j, minY);
                    maxX=max(i, maxX);
                    maxY=max(j, maxY);
                }
            }
        }
    }
    for(int i=minX; i<=maxX; i++){
        for(int j=minY; j<=maxY; j++){
            cout << update_map[i][j];
        }
        cout << '\n';
    }
    return 0;
}

처음 생각한 것과는 반대로 해당 좌표가 섬이고 해당 좌표의 왼, 오, 위, 아래가 섬이면 어차피 섬으로 유지된다는 것을 이용함

해당 좌표의 왼, 오, 위, 아래가 섬이 있다면 count를 증가시키고 count가 2, 3, 4이면 update 해주는 것으로 구현

 

'스터디 > 알고리즘' 카테고리의 다른 글

[백준 1713] 후보 추천하기  (0) 2022.01.18
[백준 5597] 과제 안 내신 분...?  (0) 2022.01.13
[백준 2164] 카드2  (0) 2022.01.10
[백준 22864] 피로도  (0) 2022.01.10
[백준 2606] 바이러스  (0) 2022.01.10
profile

Devlog

@덩이

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그