UVA-297 Quadtrees (四分树)
题目大意:求将两个以四分树形式给出的大方格合并后的小黑方格的个数。
题目分析:比较简单的四分树。
代码如下:
# include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; char p[1000]; int mark[35][35],ans; void draw(int &cur,int r,int c,int w)///参数为引用保证递归进行到底 { char ch=p[cur++]; if(ch=='p'){ draw(cur,r,c+w/2,w/2); draw(cur,r,c,w/2); draw(cur,r+w/2,c,w/2); draw(cur,r+w/2,c+w/2,w/2); }else if(ch=='f'){ for(int i=r;i<r+w;++i) for(int j=c;j<c+w;++j) if(!mark[i][j]) mark[i][j]=1,++ans; } } int main() { int T; scanf("%d",&T); while(T--) { memset(mark,0,sizeof(mark)); ans=0; for(int i=0;i<2;++i){ int t=0; scanf("%s",p); draw(t,0,0,32); } printf("There are %d black pixels.\n",ans); } return 0; }