没看解答敲了一遍,发现自己题目的理解能力有点差
1 #include2 #include 3 4 using namespace std; 5 6 struct Node 7 { 8 char value; 9 Node* ch1; 10 Node* ch2; 11 Node* ch3; 12 Node* ch4; 13 14 Node():ch1(NULL),ch2(NULL),ch3(NULL),ch4(NULL){} 15 }; 16 17 Node* newnode() { return new Node();} 18 19 Node* builtree(Node* root) 20 { 21 char v; 22 cin>>v; 23 24 if(root==NULL) 25 root=newnode(); 26 27 if(v=='e') 28 { 29 root->value='e'; 30 return root; 31 } 32 33 if(v=='f') 34 { 35 root->value='f'; 36 return root; 37 } 38 else 39 { 40 root->value='p'; 41 42 root->ch1=builtree(root->ch1); 43 root->ch2=builtree(root->ch2); 44 root->ch3=builtree(root->ch3); 45 root->ch4=builtree(root->ch4); 46 } 47 48 return root; 49 } 50 51 void _builtree(Node*& root) //使用引用,不然不能改变root 52 { 53 char v; 54 cin>>v; 55 56 if(root==NULL) 57 root=newnode(); 58 59 if(v=='e') 60 { 61 root->value='e'; 62 return; 63 } 64 65 if(v=='f') 66 { 67 root->value='f'; 68 return; 69 } 70 else 71 { 72 root->value='p'; 73 74 _builtree(root->ch1); 75 _builtree(root->ch2); 76 _builtree(root->ch3); 77 _builtree(root->ch4); 78 } 79 } 80 81 void reset(Node* root) {root->ch1=root->ch2=root->ch3=root->ch4=NULL;} 82 83 Node* merge(Node* root1,Node* root2) 84 { 85 if(root2->value=='f') 86 { 87 reset(root1); 88 root1->value='f'; 89 } 90 if(root2->value=='p') 91 { 92 if(root1->value=='e') root1=root2; 93 if(root1->value=='p') 94 { 95 root1->ch1=merge(root1->ch1,root2->ch1); 96 root1->ch2=merge(root1->ch2,root2->ch2); 97 root1->ch3=merge(root1->ch3,root2->ch3); 98 root1->ch4=merge(root1->ch4,root2->ch4); 99 }100 }101 102 return root1;103 }104 105 106 107 void print_tree(Node* root)108 {109 if(root==NULL) return;110 111 printf("%c ",root->value);112 113 print_tree(root->ch1);114 print_tree(root->ch2);115 print_tree(root->ch3);116 print_tree(root->ch4);117 }118 119 120 121 int main()122 {123 Node* root1=NULL;124 Node* root2=NULL;125 126 _builtree(root1);127 _builtree(root2);128 129 root1=merge(root1,root2);130 131 print_tree(root1);132 cout<
虽然能实现四叉树的合并但并不能算出像素:-(
然后看答案后又敲了遍
1 #include2 #include 3 #include 4 5 using namespace std; 6 7 const int len=32; 8 const int maxn=10000; 9 int w[len][len];10 char buf[maxn];11 int cnt;12 13 14 void draw(int r,int c,int length,int& p)15 {16 char ch=buf[p++];17 18 if(ch=='p')19 {20 draw(r+length/2,c,length/2,p);21 draw(r,c,length/2,p);22 draw(r,c+length/2,length/2,p);23 draw(r+length/2,c+length/2,length/2,p);24 }25 if(ch=='f') //当树满足条件是,相当于递归基26 {27 for(int i=c;i >T;43 44 while(T--)45 {46 memset(w,0,sizeof(w));47 48 cnt=0;49 int r=0;50 int c=0;51 int p=0;52 53 scanf("%s",buf);54 draw(r,c,len,p);55 56 scanf("%s",buf);57 p=0;58 draw(r,c,len,p);59 60 printf("%d\n", cnt);61 }62 }