#20061 모노미노도미노2
문제 제목만큼이나 까다로운 문제였다. 특히 좌표를 다루는 게..
Solution
moveBlock(t,y,x)
, Blue칸에 있는 블록이 합쳐지는 mergeBlue()
, Green칸에 있는 블록이 합쳐지는 mergeGreen()
이 있다🎲1. Move Block
1*1 t1모양
if (t == 1) {
//move to blue
int nx = x;
while (nx < mapSize - 1 && !map[y][nx + 1])
nx++;
map[y][nx] = 1;
//move to green
int ny = y;
while (ny < mapSize - 1 && !map[ny + 1][x])
ny++;
map[ny][x] = 1;
}
1*2 t2모양
//1*2 block
else if (t == 2) {
//move to blue
int nx = x + 1;
while (nx < mapSize - 1 && !map[y][nx + 1])
nx++;
map[y][nx] = 1; map[y][nx - 1] = 1;
//move to green
int ny = y + 1;
while (ny < mapSize - 1 && !map[ny + 1][x] && !map[ny + 1][x + 1])
ny++;
map[ny][x] = 1; map[ny][x + 1] = 1;
}
2*1 t3모양
//2*1 block
else if (t == 3) {
//move to blue
int nx = x + 1;
while (nx < mapSize - 1 && !map[y][nx + 1] && !map[y + 1][nx + 1])
nx++;
map[y][nx] = 1; map[y + 1][nx] = 1;
//move to green
int ny = y + 1;
while (ny < mapSize - 1 && !map[ny + 1][x])
ny++;
map[ny][x] = 1; map[ny - 1][x] = 1;
}
🎲2. Merge Blue/Green
(blue칸만을 본다)
//count number of merging cols
for (int x = 6; x < mapSize; x++) {
for (int y = 0; y <= 3; y++) {
if (map[y][x] == 0) break;
if (y == 3) {
start = x;
remove++;
}
}
}
//merge from 4 to 9 clos
if (start > -1) {
for (int x = start - remove; x >= 4; x--) {
for (int y = 0; y <= 3; y++) {
map[y][x + remove] = map[y][x];
map[y][x] = 0;
}
}
}
int cols = 0;
for (int x = 4; x <= 5; x++) {
for (int y = 0; y <= 3; y++) {
if (map[y][x] == 1) {
cols++; break;
}
}
}
if (cols > 0) {
for (int x = 9 - cols; x >= 4; x--) {
for (int y = 0; y <= 3; y++) {
map[y][x + cols] = map[y][x];
}
}
for (int x = 4; x <= 5; x++)
for (int y = 0; y <= 3; y++)
map[y][x] = 0;
}
Sorce Code
#include <iostream>
using namespace std;
const int mapSize = 10;
int map[mapSize][mapSize];
int score = 0;
void mergeBlue() {
int start = -1;
int remove = 0;
//count number of merging cols
for (int x = 6; x < mapSize; x++) {
for (int y = 0; y <= 3; y++) {
if (map[y][x] == 0) break;
if (y == 3) {
start = x;
remove++;
}
}
}
score += remove;
//merge from 4 to 9 clos
if (start > -1) {
for (int x = start - remove; x >= 4; x--) {
for (int y = 0; y <= 3; y++) {
map[y][x + remove] = map[y][x];
map[y][x] = 0;
}
}
}
//print();
//check 4-5 cols
int cols = 0;
for (int x = 4; x <= 5; x++) {
for (int y = 0; y <= 3; y++) {
if (map[y][x] == 1) {
cols++; break;
}
}
}
//merge from 4 to 9 cols
if (cols > 0) {
for (int x = 9 - cols; x >= 4; x--) {
for (int y = 0; y <= 3; y++) {
map[y][x + cols] = map[y][x];
}
}
for (int x = 4; x <= 5; x++)
for (int y = 0; y <= 3; y++)
map[y][x] = 0;
}
//print();
}
void mergeGreen() {
int start = -1;
int remove = 0;
//count number of merging rows
for (int y = 6; y < mapSize; y++) {
for (int x = 0; x <= 3; x++) {
if (map[y][x] == 0) break;
if (x == 3) {
start = y;
remove++;
}
}
}
score += remove;
//merge from 4 to 9 rows
if (start > -1) {
for (int y = start - remove; y >= 4; y--) {
for (int x = 0; x <= 3; x++) {
map[y+remove][x] = map[y][x];
map[y][x] = 0;
}
}
}
//check 4-5 rows
int rows = 0;
for (int y = 4; y <= 5; y++) {
for (int x = 0; x <= 3; x++) {
if (map[y][x] == 1) {
rows++; break;
}
}
}
//merge from 4 to 9 rows
if (rows > 0) {
for (int y = 9 - rows; y >= 4; y--) {
for (int x = 0; x <= 3; x++) {
map[y+rows][x] = map[y][x];
}
}
for (int y = 4; y <= 5; y++)
for (int x = 0; x <= 3; x++)
map[y][x] = 0;
}
//print();
}
void moveBlock(int t, int y, int x) {
//1*1 block
if (t == 1) {
//move to blue
int nx = x;
while (nx < mapSize - 1 && !map[y][nx + 1])
nx++;
map[y][nx] = 1;
//move to green
int ny = y;
while (ny < mapSize - 1 && !map[ny + 1][x])
ny++;
map[ny][x] = 1;
}
//1*2 block
else if (t == 2) {
//move to blue
int nx = x + 1;
while (nx < mapSize - 1 && !map[y][nx + 1])
nx++;
map[y][nx] = 1; map[y][nx - 1] = 1;
//move to green
int ny = y + 1;
while (ny < mapSize - 1 && !map[ny + 1][x] && !map[ny + 1][x + 1])
ny++;
map[ny][x] = 1; map[ny][x + 1] = 1;
}
//2*1 block
else if (t == 3) {
//move to blue
int nx = x + 1;
while (nx < mapSize - 1 && !map[y][nx + 1] && !map[y + 1][nx + 1])
nx++;
map[y][nx] = 1; map[y + 1][nx] = 1;
//move to green
int ny = y + 1;
while (ny < mapSize - 1 && !map[ny + 1][x])
ny++;
map[ny][x] = 1; map[ny - 1][x] = 1;
}
}
void input() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
int t, y, x;
cin >> t >> y >> x;
moveBlock(t, y, x);
mergeBlue();
mergeGreen();
}
}
int countBlock() {
int cnt = 0;
for (int y = 0; y <= 3; y++)
for (int x = 4; x < mapSize; x++)
cnt += map[y][x];
for (int y = 4; y < mapSize; y++)
for (int x = 0; x <= 3; x++)
cnt += map[y][x];
return cnt;
}
int main() {
input();
cout << score << endl;
cout << countBlock() << endl;
return 0;
}