RT,做法与dalao们不同...注释已给出
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, d;
};
int m, n, l[15], x, y, px, py, maxleaf = 1, cnt;
char a[5010][5010];
bool del[5010][5010];
queue <node> q;
inline int leaf (int x) { return maxleaf >> (x - 1); }
//某行第一个节点的叶子数
inline int space (int x) {
if (x == m) return 0;
return (leaf(x) >> 1) * 3 - 1;
}
//第x行所需要留出的空格
inline char dif (char x) {
if (x == '\\') return '/';
if (x == 'o') return 'o';
return '\\';
}
//不同的字符
void putc (int x, int y, int xx, int yy) {
while (x != xx && y != yy) {
++x, --y;
a[x][y] = '/';
}
}
//填充两节点间路径
void copy (int x, int y, int d=-1) {
if (x == l[m]) return ;
int tx = x, ty = y, ny;
while (a[tx][ty]) {
++tx, ty += d;
if (d > 0) ny = y-(ty-y);
else ny = y+(y-ty);
a[tx][ny] = dif (a[tx][ty]);
if (a[tx][ty] == 'o')
q.push ((node) {tx, y+(y-ty), -d});
}
}
//以(x,y)为对称轴复制
void build () {
while (!q.empty ()) {
node fro = q.front ();
q.pop ();
int x = fro.x, y = fro.y, d = fro.d;
copy (x, y, d);
}
}
//建立满二叉树
void delup (int x, int y) {
int d;
a[x][y] = ' ';
if (a[x-1][y+1] == '/') d = 1;
if (a[x-1][y-1] == '\\') d = -1;
while (a[x][y] != 'o') {
a[x][y] = ' ';
--x, y += d;
}
}
//向上删除
void deldown (int x, int y, int d) {
a[x][y] = ' ';
if (x == l[m]) return ;
while (a[x][y] >= ' ') {
if (a[x][y] == 'o') deldown (x, y, -d);
a[x][y] = ' ';
++x, y += d;
}
}
//向下删除(遇到分支则递归同时处理)
int main () {
scanf ("%d%d", &m, &n);
maxleaf = 1 << (m-1);
for (int i=1, j=1; j<=m; ++j) {
l[j] = x = i, y = space (j) + 1;
q.push ((node) {x, y, -1});
if (j != 1) putc (px, py, x, y);
a[x][y] = 'o';
i += (space(j) - space (j+1));
px = x, py = y;
}
//建立一条链,以供copy函数复制为二叉树
build ();
for (int i=1; i<=n; ++i) {
scanf ("%d%d", &x, &y);
cnt = 0;
for (int j=1; j<=space (1) * 2+1; ++j) {
if (a[l[x]][j] == 'o' || del[l[x]][j]) ++cnt;
if (cnt == y) {
del[l[x]][j] = true;
delup (l[x], j);
deldown (l[x], j, 1);
deldown (l[x], j, -1);
break ;
}
}
}
//删点
for (int i=1; i<=l[m]; ++i) {
for (int j=1; j<=space (1) * 2+1; ++j) {
if (a[i][j] > ' ')
putchar (a[i][j]);
else putchar (' ');
}
putchar ('\n');
}
//输出
}