백준알고리즘

미네랄

먼지의삶 2019. 9. 10. 01:54

#include <iostream>
#include <string.h>
#include <queue>
using namespace std;

int r, c;
char map[111][111];
bool dist[111][111];
int dx[4] = { -1, 1 , 0 , 0 };
int dy[4] = { 0, 0, -1, 1 };

int main() {
	ios::sync_with_stdio(false);
	cin >> r >> c;
	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			cin >> map[i][j];
		}
	}
	int n;
	cin >> n;
	bool left = true;
	while (n--) {
		int height;
		cin >> height;
		height = r - height + 1;
		// del
		if (left == true) {
			for (int i = 1; i <= c; i++) {
				if (map[height][i] == 'x') {
					map[height][i] = '.';
					break;
				}
			}
		}
		else {
			for (int i = c; i >= 1; i--) {
				if (map[height][i] == 'x') {
					map[height][i] = '.';
					break;
				}
			}
		}
		left = !left;

		// check
		memset(dist, false, sizeof(dist));
		for (int j = 1; j <= c; j++) {
			if (map[r][j] == 'x') {
				queue<pair<int, int> > q;
				q.push(make_pair(r, j));
				dist[r][j] = true;
				while (!q.empty()) {
					int x = q.front().first;
					int y = q.front().second;
					q.pop();
					for (int k = 0; k < 4; k++) {
						int nx = x + dx[k];
						int ny = y + dy[k];
						if (nx >= 1 && nx <= r && ny >= 1 && ny <= c) {
							if (!dist[nx][ny] && map[nx][ny] == 'x') {
								dist[nx][ny] = true;
								q.push(make_pair(nx, ny));
							}
						}
					}
				}
			}
		}

		int down = r;
		for (int j = 1; j <= c; j++) {
			for (int i = r; i >= 1; i--) {
				if (map[i][j] == 'x' && dist[i][j] == false) {
					int col = r - i;
					for (int k = i + 1; k <= r; k++) {
						if (dist[k][j] == true) {
							col = k - i - 1;
							break;
						}
					}
					if (down > col) {
						down = col;
					}
				}
			}
		}
		for (int j = 1; j <= c; j++) {
			for (int i = r; i >= 1; i--) {
				if (i + down <= r && down != 0 && map[i][j] == 'x' && dist[i][j] == false) {
					map[i + down][j] = map[i][j];
					map[i][j] = '.';
				}
			}
		}
	}

	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			cout << map[i][j];
		}
		cout << '\n';
	}
	return 0;
}

풀면서 진짜 구현하기 복잡했던 문제가 아닌가 싶다.

처음 막대로인해 맵이사라지는거까지는 기초적인부분이라 어렵지않았지만, 망설여지고 어려웠던부분은 아무래도

클러스터들이 나뉘는 기준과 내려갈때 클러스터 단위로 내려와서 다른하나가 다른클러스터에 닿으면 멈춰야한다는점이 구현하기 어려웠던것같다.