UVA297 四分树 Quadtrees

简介: UVA297 四分树 Quadtrees

题目描述


20210501220412684.png

输入


3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe


输出


There are 640 black pixels.
There are 512 black pixels.
There are 384 black pixels.


思路:二叉树+模拟 给出了先序顺序,根据先序把二叉树用图表示出来,两个图相加,统计黑色小块的个数.

参考代码

#include<bits/stdc++.h>
using namespace std;
const int len = 32;//
const int maxn = 1024 + 10;//最多的节点数.32*32=1024 
string str;//接受先序字符串
int arr[len][len],cnt,n;//黑白图像的大小 
//把字符串先序填充到arr图中,边长为w的缓冲区中
void draw(string &s,int &p, int r,int c,int w){
  char ch = s[p++];
  if(ch == 'p'){
    draw(s,p,r,c+w/2,w/2);
    draw(s,p,r,c,w/2);
    draw(s,p,r+w/2,c,w/2);
    draw(s,p,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(!arr[i][j]){//两张图进行相加时如果同一区域已经标记了为黑色则不再修改其值,如果一黑一白则修改为黑色. 
          arr[i][j] = 1;
          cnt++;//对黑色块进行计数. 
        }
      }
    }
  }
}
int main()
{
  cin>>n;
  while(n--){
    memset(arr,0,sizeof(arr));
    cnt = 0;
    for(int i = 0; i < 2; i++){
      cin>>str;
      int p = 0;
      draw(str,p,0,0,len);
    }
    //There are 640 black pixels.
    cout<<"There are "<<cnt<<" black pixels."<<endl;
  }
  return 0;
}
相关文章
|
2月前
|
人工智能 BI
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
33 0
|
9月前
|
人工智能 算法 测试技术
华为机试HJ52:计算字符串的距离(动态规划)
华为机试HJ52:计算字符串的距离(动态规划)
|
11月前
UVa10075 - Airlines(所有点对之间的最短距离)
UVa10075 - Airlines(所有点对之间的最短距离)
23 0
|
算法 C++
【每日算法Day 64】LeetCode 861. 翻转矩阵后的得分
【每日算法Day 64】LeetCode 861. 翻转矩阵后的得分
|
Go
[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim
有n个点,其中,每个点给出位置坐标( x , y ) 以及高度z ,两点之间的距离为两点之间的欧几里得距离 两点之间建立一条路的代价为两点之间的高度差,问将n 个点联通的情况下,求出最大的cost/dis
112 0
HDOJ/HDU 2566 统计硬币(公式~遍历~)
HDOJ/HDU 2566 统计硬币(公式~遍历~)
122 0
|
Windows
BZOJ 1303: [CQOI2009]中位数图【前缀和】
1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2737  Solved: 1698[Submit][Status][Discuss] Description 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。
915 0
|
人工智能
Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table.
899 0
|
机器学习/深度学习
LCM性质 + 组合数 - HDU 5407 CRB and Candies
CRB and Candies Problem's Link  Mean:  给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...C(n,n))的值,(n
998 0

热门文章

最新文章