297 - Quadtrees

All about problems in Volume 2. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

killerwife
New poster
Posts: 11
Joined: Tue Jan 28, 2014 2:16 am

297 - Quadtrees

Post by killerwife »

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int pictureA[1024];
    int pictureB[1024];
    char current;
    int cases;
    scanf("%d",&cases);
    fgetc(stdin);
    for(;cases>0;cases--)
    {
        int multiplier=1024;
        int i;
        int hlbka=-1;
        int k[5];
        int blackpix=0;
        for(i=0;i<5;i++)
        {
            k[i]=0;
        }
        i=0;
        while((current=fgetc(stdin))!='\n')
        {
            if(current=='p')
            {
                multiplier/=4;
                hlbka++;
            }
            else if(current=='f')
            {
                int l;
                for(l=0;l<multiplier;l++)
                {
                    pictureA[i]=1;
                    i++;
                }
                k[hlbka]++;
            }
            else
            {
                int l;
                for(l=0;l<multiplier;l++)
                {
                    pictureA[i]=0;
                    i++;
                }
                k[hlbka]++;
            }
            while(k[hlbka]>3)
            {
                k[hlbka]=0;
                multiplier*=4;
                hlbka--;
                k[hlbka]++;
            }
        }
        multiplier=1024;
        hlbka=-1;
        for(i=0;i<5;i++)
        {
            k[i]=0;
        }
        i=0;
        while((current=fgetc(stdin))!='\n'&&current!=EOF)
        {
            if(current=='p')
            {
                multiplier/=4;
                hlbka++;
            }
            else if(current=='f')
            {
                int l;
                for(l=0;l<multiplier;l++)
                {
                    pictureB[i]=1;
                    i++;
                }
                k[hlbka]++;
            }
            else
            {
                int l;
                for(l=0;l<multiplier;l++)
                {
                    pictureB[i]=0;
                    i++;
                }
                k[hlbka]++;
            }
            while(k[hlbka]>3)
            {
                k[hlbka]=0;
                multiplier*=4;
                hlbka--;
                k[hlbka]++;
            }
        }
        for(i=0;i<1024;i++)
        {
            if(pictureA[i]==1||pictureB[i]==1)
            {
                blackpix++;
            }
        }
        printf("There are %d black pixels.\n",blackpix);
    }
    return 0;
}
I have tested lots of test cases and I dont know why i dont have WA. Help appreciated.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Quadtrees - 297

Post by brianfry713 »

Try running your code on the sample input.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 2 (200-299)”