시뮬레이션 문제다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <iostream> using namespace std; char map[30][31], q, arr[31]; int t, N, M, Q, i, j, k, pad; int x, y, nx, ny, cnt, ccnt = 0, dqs, dqe, qs, qqs, qe, mv, f; int c[30][30], dx[] = { 0,0,1,-1 }, dy[] = { 1,-1,0,0 }; int qq[900][2]; void drop() { for (i = 0; i < M; ++i) { pad = 0; for (j = N - 1; j >= 0; --j) { if (map[j][i] == '#') pad++; else if (pad) { map[j + pad][i] = map[j][i]; map[j][i] = '#'; } } } } void _up() { f = 0; for (i = 0; i < M; ++i) if (map[0][i] != '#') break; if (i != M) return; for (i = 0; i < N - 1; ++i) for (j = 0; j < M; ++j) map[i][j] = map[i + 1][j]; for (j = 0; j < M; ++j) { map[N - 1][j] = arr[j]; if (arr[j] == '#') f = 1; } if (f) drop(); } void _left() { for (i = 0; i < N; ++i) { pad = 0; for (j = 0; j < M; ++j) { if (map[i][j] == '#') pad++; else if (pad) { map[i][j - pad] = map[i][j]; map[i][j] = '#'; } } } } void _right() { for (i = 0; i < N; ++i) { pad = 0; for (j = M - 1; j >= 0; --j) { if (map[i][j] == '#') pad++; else if (pad) { map[i][j + pad] = map[i][j]; map[i][j] = '#'; } } } } void _del() { ccnt++; dqs = dqe = 0; mv = 0; for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) if (map[i][j] != '#' && c[i][j] != ccnt) { qqs = qs = qe = dqe; qq[qe][0] = i; qq[qe++][1] = j; cnt = 0; c[i][j] = ccnt; while (qs < qe) { cnt++; x = qq[qs][0]; y = qq[qs++][1]; for (k = 0; k < 4; ++k) { nx = x + dx[k]; ny = y + dy[k]; if (nx < 0 || ny < 0 || nx >= N || ny >= M || map[nx][ny] != map[x][y] || c[nx][ny] == ccnt) continue; qq[qe][0] = nx; qq[qe++][1] = ny; c[nx][ny] = ccnt; } } if (cnt > mv) { mv = cnt; dqs = qqs; dqe = qe; } else if (cnt == mv) { dqe = qe; } } while (dqs < dqe) map[qq[dqs][0]][qq[dqs][1]] = '#', dqs++; drop(); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> t; for (int tc = 1; tc <= t; ++tc) { cin >> N >> M >> Q; for (i = 0; i < N; ++i) cin >> map[i]; while (Q--) { cin >> q; if (q == 'U') cin >> arr, _up(); else if (q == 'L') _left(); else if (q == 'R') _right(); else if (q == 'D') _del(); } cout << '#' << tc << '\n'; for (i = 0; i < N; ++i) cout << map[i] << '\n'; cout << '\n'; } return 0; } | cs |
'SWEA' 카테고리의 다른 글
1824. 혁진이의 프로그램 검증 (0) | 2018.10.08 |
---|---|
2112. 보호 필름 (0) | 2018.10.08 |
핀볼 런타임 에러나는 TC (0) | 2018.10.07 |
5644. 무선 충전 (0) | 2018.09.27 |
5648. 원자 소멸 시뮬레이션 (0) | 2018.09.21 |