Computer Science/Algorithm Problem

백준] 5212 - 지구 온난화(COCI 2012/2013)

TwinParadox 2018. 4. 20. 00:09
728x90

시간 제한 : 1초

메모리 제한 : 128MB




입력

첫째 줄에 지도의 크기 R과 C (1 ≤ R, C ≤ 10)가 주어진다. 다음 R개 줄에는 현재 지도가 주어진다.




출력

50년 후의 지도를 출력한다.




소스코드

#include <iostream>
#include <string>
using namespace std;
int main(void)
{
	int r, c, startR, startC, endR, endC;
	string arr[10], next[10];
	cin >> r >> c;
	for (int i = 0; i < r; i++)
	{
		cin >> arr[i];
		next[i] = arr[i];
	}
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			if ((char)arr[i][j] == 'X')
			{
				int cnt = 0;
				if (i - 1 < 0)
					cnt++;
				else
					if ((char)arr[i - 1][j] == '.')
						cnt++;
				if (i + 1 >= r)
					cnt++;
				else
					if ((char)arr[i + 1][j] == '.')
						cnt++;

				if (j - 1 < 0)
					cnt++;
				else
					if ((char)arr[i][j - 1] == '.')
						cnt++;
				if (j + 1 >= c)
					cnt++;
				else
					if ((char)arr[i][j + 1] == '.')
						cnt++;
				if (cnt >= 3)
					next[i][j] = '.';
			}
		}
	}

	startR = r - 1, startC = c - 1;
	endR = endC = 0;
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			if (next[i][j] == 'X')
			{
				startR = startR > i ? i : startR;
				startC = startC > j ? j : startC;
				endR = endR < i ? i : endR;
				endC = endC < j ? j : endC;
			}
		}
	}
	for (int i = startR; i <= endR; i++)
	{
		for (int j = startC; j <= endC; j++)
			cout << next[i][j];
		cout << '\n';
	}
}




Tip

단순 구현문제이며, 필자는 50년 뒤 지도를 그릴 때 지도 범위 선정에서 실수를 해서 틀렸었다. 최소 범위로 출력해야 한다는 사실을 명심하자.


728x90
728x90