본문 바로가기

알고리즘 문제

[C/C++ 백준 5212번] 지구온난화 (Silver 2)

https://www.acmicpc.net/problem/5212

 

5212번: 지구 온난화

문제 푸르고 아름다운 남해에는 많은 섬이 장관을 이루고 있다. 그림이 아니면 볼 수 없을 것 같은 아름다운 장관을 실제로 볼 수 있는 다도해로 상근이는 여행을 떠났다. 다도해에 도착한 상근�

www.acmicpc.net

land1, land2로 만들어놓고, land1을 이용해 land2를 그리자. X를 기준으로 주변 X개수를 따져, land2에 X를 그려주면 되는 문제이다.

 

코드는 다음과 같다.

#include <cstdio>
#include <algorithm>
using namespace std;
int main(void){
	int a, b, cnt;
	int minx=9999, miny=9999, maxx=-1, maxy=-1;
	char land1[12][12]={}, land2[12][12]={};
	scanf("%d %d",&a,&b);
	for(int i=1; i<=a; i++){
		for(int j=1; j<=b; j++){
			scanf(" %c",&land1[i][j]);
			land2[i][j]='.';
		}
	}
	for(int i=1; i<=a; i++){
		for(int j=1; j<=b; j++){
			cnt = 0;
			if(land1[i][j]!='.'){
				if(land1[i][j-1]=='X')
					cnt++;
				if(land1[i][j+1]=='X')
					cnt++;
				if(land1[i-1][j]=='X')
					cnt++;
				if(land1[i+1][j]=='X')
					cnt++;
				if(cnt==2 || cnt==3 || cnt==4){
					land2[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++){
			printf("%c",land2[i][j]);
		}
		printf("\n");
	}
}