백준/기타

[백준/C++] 18111번: 마인크래프트

Koalitsiya 2023. 4. 19. 14:52

문제

 

 

풀이

 모든 땅의 높이가 각각 0,1,2,...,254,255,256으로 맞춰지는 시간을 구하여 그 중 최소 시간과 최소 시간이 나올 때의 땅의 높이를 구해 출력하면 된다.

#include <iostream>
#include <vector>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, m, b;
	int board[501][501];
	int minTime = 0x7f7f7f7f;
	int maxH = -1;

	cin >> n >> m >> b;

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> board[i][j];

	for (int height = 0; height <= 256; height++) {
		int removeBlock = 0, deployBlock = 0;

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int h = board[i][j] - height;
				if (h > 0) removeBlock += h;
				else if (h < 0) deployBlock -= h;
			}
		}

		if (removeBlock + b >= deployBlock) {
			int t = 2 * removeBlock + deployBlock;
			if (t <= minTime) {
				minTime = t;
				maxH = height;
			}
		}
	}

	cout << minTime << " " << maxH;

	return 0;
}