zoukankan      html  css  js  c++  java
  • uva297

    做这道题的时候傻X错误百出。。。在计算总和上错了好久。还把=写成了==。。。。蛋疼无比

    题意就是合并两颗4叉树。 然后按照要求计算值。。主要用了个递归 。 没有像网上其他一些解法用结构指针什么的。。单用了数组

    题目

    Quadtrees 

    A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the image is represented by a parent node, while the four quadrants are represented by four child nodes, in a predetermined order.

    Of course, if the whole image is a single color, it can be represented by a quadtree consisting of a single node. In general, a quadrant needs only to be subdivided if it consists of pixels of different colors. As a result, the quadtree need not be of uniform depth.

    A modern computer artist works with black-and-white images of tex2html_wrap_inline34 units, for a total of 1024 pixels per image. One of the operations he performs is adding two images together, to form a new image. In the resulting image a pixel is black if it was black in at least one of the component images, otherwise it is white.

    This particular artist believes in what he calls the preferred fullness: for an image to be interesting (i.e. to sell for big bucks) the most important property is the number of filled (black) pixels in the image. So, before adding two images together, he would like to know how many pixels will be black in the resulting image. Your job is to write a program that, given the quadtree representation of two images, calculates the number of pixels that are black in the image, which is the result of adding the two images together.

    In the figure, the first example is shown (from top to bottom) as image, quadtree, pre-order string (defined below) and number of pixels. The quadrant numbering is shown at the top of the figure.

    Input Specification

    The first line of input specifies the number of test cases (N) your program has to process.

    The input for each test case is two strings, each string on its own line. The string is the pre-order representation of a quadtree, in which the letter 'p' indicates a parent node, the letter 'f' (full) a black quadrant and the letter 'e' (empty) a white quadrant. It is guaranteed that each string represents a valid quadtree, while the depth of the tree is not more than 5 (because each pixel has only one color).

    Output Specification

    For each test case, print on one line the text 'There are X black pixels.', where X is the number of black pixels in the resulting image.

    Example Input

    3
    ppeeefpffeefe
    pefepeefe
    peeef
    peefe
    peeef
    peepefefe

    Example Output

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

    代码
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <string.h>
      4 using namespace std;
      5 const int maxn = 4096;
      6 char tree1[maxn];
      7 char tree2[maxn];
      8 char newtree[maxn];
      9 int pn=0;
     10 int x=0;
     11 int cal(int ans,int mul)
     12 {
     13   //  cout<<x<<" "<<ans<<" "<<mul<<endl;
     14 
     15     if(x>=strlen(newtree))return 0;
     16     if(newtree[x]=='e')
     17         return 0;
     18     if(newtree[x] == 'f')
     19         return mul;
     20     if(newtree[x]=='p')
     21     {
     22         for(int i=1;i<=4;i++)
     23         {
     24             x++;
     25           // cout<<"before"<<"i "<<i<<"x:  "<<x<<"ans  "<<ans<<endl;
     26             ans+=cal(0,mul/4);  //此处不要写成 ans+=cal(ans,mul/4)我蛋疼了好久。。否则会多加一点
     27          //  cout<<"i "<<i<<"x:  "<<x<<"ans  "<<ans<<endl;
     28         }
     29       //  cout<<"return "<<endl;
     30        return ans;
     31     }
     32 
     33     return 0;
     34 }
     35 void solve()
     36 {
     37     int p1=0,p2=0;
     38     while(p1<strlen(tree1)&&p2<strlen(tree2))
     39     {
     40         if(p1==strlen(tree1) || p2==strlen(tree2))break;
     41        // cout<<p1<<" "<<p2<<endl;
     42         if(tree1[p1]=='p' && tree2[p2]=='p')
     43         {
     44 
     45             newtree[pn++]='p';
     46             p1++;p2++;
     47         }
     48         else if(tree1[p1]=='f')
     49         {
     50 
     51             newtree[pn++]='f';
     52             if(tree2[p2]=='p')p2+=5;
     53             else p2++;
     54             p1++;
     55         }
     56         else if(tree2[p2]=='f')
     57         {
     58 
     59             newtree[pn++]='f';
     60             if(tree1[p1]=='p')p1+=5;
     61             else p1++;
     62             p2++;
     63         }
     64         else if(tree1[p1]=='e'&&tree2[p2]=='e')
     65         {
     66              // cout<<"here2"<<endl;
     67             newtree[pn++]='e';
     68             p1++;p2++;
     69         }
     70         else if(tree1[p1]=='p'&&tree2[p2]=='e')
     71         {
     72           // cout<<"here3"<<endl;
     73             for(int i=0;i<=4;i++)
     74                 newtree[pn++]=tree1[p1+i];
     75             p1+=5;
     76             p2++;
     77         }
     78         else if(tree2[p2]=='p'&&tree1[p1]=='e')
     79         {
     80            // cout<<"here4"<<endl;
     81                for(int i=0;i<=4;i++)
     82                 newtree[pn++]=tree2[p2+i];
     83             p1++;
     84             p2+=5;
     85         }
     86 
     87        // cout<<newtree<<endl;
     88     }
     89      for(int i=p1+1;i<strlen(tree1)-1;i++)
     90         newtree[pn++]=tree1[i];
     91     for(int i=p2+1;i<strlen(tree2)-1;i++)
     92         newtree[pn++]=tree2[i];
     93    // cout<<newtree<<endl;
     94 }
     95 
     96 int main()
     97 {
     98     int tst;
     99     cin>>tst;
    100     getchar();
    101     int finalans=0;
    102     while(tst--)
    103     {
    104         gets(tree1);
    105         gets(tree2);
    106 
    107         solve();
    108        // cout<<newtree<<strlen(newtree)<<endl;
    109 
    110         finalans=cal(0,64*16);
    111         cout<<"There are "<<finalans<<" black pixels."<<endl;
    112         pn=0;
    113         memset(tree1,'',sizeof(tree1));
    114         memset(tree2,'',sizeof(tree2));
    115         memset(newtree,'',sizeof(newtree));
    116         x=0;
    117         finalans=0;
    118     }
    119 
    120     return 0;
    121 }
  • 相关阅读:
    关于如何Debug进MVC3源代码
    浏览文件按钮
    C#多线程学习(五) 多线程的自动管理(定时器)
    记录总数
    Json对象格式化字符串输出
    数据与通信之WebRequest.Web
    ASP.NET MVC3中的ViewBag动态性
    SQL Server 2005的XML数据修改语言(XML DML)
    SOCKET与TCP/IP与HTTP的关系
    WPF绑定方式
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3438277.html
Copyright © 2011-2022 走看看