知识点:多叉树的递归遍历
虽然是多叉树,其实递归遍历也是和二叉树一样的,道理是一样的,这里要注意,我们输入一个节点的数据,然后就要去递归这个节点下面的内容,这才算对整个节点的子树操作完成了,以前做过这个题,15分钟过了
- #include
-
- using namespace std;
-
- int a[35][35];
-
- void solve(char ch, int x, int y, int level) {
- if (ch != 'p') {
- for (int i = x; i < x + (1 << level); i++) {
- for (int j = y; j < y + (1 << level); j++) {
- a[i][j] += (ch == 'f' ? 1 : 0);
- }
- }
- } else {
- char t;
- cin >> t; solve(t, x, y, level - 1);
- cin >> t; solve(t, x + (1 << (level - 1)), y, level - 1);
- cin >> t; solve(t, x, y + (1 << (level - 1)), level - 1);
- cin >> t; solve(t, x + (1 << (level - 1)), y + (1 << (level - 1)), level - 1);
- }
- }
-
- int main() {
- int T;
- cin >> T;
- while (T--) {
- memset(a, 0, sizeof(a));
- char ch;
- cin >> ch;
- solve(ch, 0, 0, 5);
- cin >> ch;
- solve(ch, 0, 0, 5);
- int ans = 0;
- for (int i = 0; i < 32; i++) {
- for (int j = 0; j < 32; j++) {
- if (a[i][j]) ans++;
- }
- }
- cout << "There are " << ans << " black pixels.\n";
- }
- return 0;
- }