#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define MAX_SIZE 10000
char decompressed[MAX_SIZE][MAX_SIZE];
void decompress(string::iterator& it, int x, int y, int size) {
// 한 글자를 검사할 때마다 반복자를 한 칸 앞으로 옮긴다.
char head = *(it++);
// 기저 사례: 첫 글자가 b 또는 w인 경우
if (head == 'b' || head == 'w') {
for (int dx = 0; dx < size; dx++)
for (int dy = 0; dy < size; dy++)
decompressed[x + dx][y + dy] = head;
}
else {
// 네 부분을 각각 순서대로 압축 해제한다.
int half = size / 2;
decompress(it, x, y, half);
decompress(it, x, y + half, half);
decompress(it, x + half, y, half);
decompress(it, x + half, y + half, half);
}
}
string reverse(string::iterator& it) {
char head = *it;
++it;
if (head == 'b' || head == 'w')
return string(1, head);
string upperLeft = reverse(it);
string upperRight = reverse(it);
string lowerLeft = reverse(it);
string lowerRight = reverse(it);
// 각각 위와 아래 조각들의 위치를 바꾼다.
return string("x") + lowerLeft + lowerRight + upperLeft + upperRight;
}
void print_map(int n) {
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++)
cout << decompressed[r][c] << " ";
cout << endl;
}
}
int main()
{
string str;
cin >> str;
string::iterator iter = str.begin();
decompress(iter, 0, 0, 16);
print_map(16);
cout << endl;
cout << "-----------After reversing---------------" << endl;
iter = str.begin();
string rstr = reverse(iter);
iter = rstr.begin();
decompress(iter, 0, 0, 16);
print_map(16);
cout << endl;
cout << "reverse image: " << rstr << endl;
}
입력
xxwwwbxwxwbbbwwxxxwwbbbwwwwbb
출력
w w w w w w w w w w w w w w b b
w w w w w w w w w w w w w w b b
w w w w w w w w w w w w b b b b
w w w w w w w w w w w w b b b b
w w w w b b b b w w w w w w w w
w w w w b b b b w w w w w w w w
w w w w b b b b w w w w w w w w
w w w w b b b b w w w w w w w w
w w b b w w w w b b b b b b b b
b b b b w w w w b b b b b b b b
w w w w w w w w b b b b b b b b
w w w w w w w w b b b b b b b b
w w w w b b b b b b b b b b b b
w w w w b b b b b b b b b b b b
w w w w b b b b b b b b b b b b
w w w w b b b b b b b b b b b b
-----------After reversing---------------
w w w w b b b b b b b b b b b b
w w w w b b b b b b b b b b b b
w w w w b b b b b b b b b b b b
w w w w b b b b b b b b b b b b
w w w w w w w w b b b b b b b b
w w w w w w w w b b b b b b b b
b b b b w w w w b b b b b b b b
w w b b w w w w b b b b b b b b
w w w w b b b b w w w w w w w w
w w w w b b b b w w w w w w w w
w w w w b b b b w w w w w w w w
w w w w b b b b w w w w w w w w
w w w w w w w w w w w w b b b b
w w w w w w w w w w w w b b b b
w w w w w w w w w w w w w w b b
w w w w w w w w w w w w w w b b
reverse image: xxwbxwwxbbwwbwbxwbwwxwwwxbbwb
Leave a comment