문제

I understand the basics of recursion, but when I encounter a problem like this one from hackerrank. I quickly get confused as to how to approach it.

Basically you have to draw a fractal ascii tree (made up of the letter Y), each layer going downward halves the number of Ys. I can't seem to get my head around the base step so I can generalize it to the other layers, like below:

____________________________________________________________________________________________________
__________________1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1___________________
___________________1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1____________________
___________________1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1____________________
____________________1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
______________________1_____1_________1_____1_________1_____1_________1_____1_______________________
_______________________1___1___________1___1___________1___1___________1___1________________________
________________________1_1_____________1_1_____________1_1_____________1_1_________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
__________________________1_____________1_________________1_____________1___________________________
___________________________1___________1___________________1___________1____________________________
____________________________1_________1_____________________1_________1_____________________________
_____________________________1_______1_______________________1_______1______________________________
______________________________1_____1_________________________1_____1_______________________________
_______________________________1___1___________________________1___1________________________________
________________________________1_1_____________________________1_1_________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
__________________________________1_____________________________1___________________________________
___________________________________1___________________________1____________________________________
____________________________________1_________________________1_____________________________________
_____________________________________1_______________________1______________________________________
______________________________________1_____________________1_______________________________________
_______________________________________1___________________1________________________________________
________________________________________1_________________1_________________________________________
_________________________________________1_______________1__________________________________________
__________________________________________1_____________1___________________________________________
___________________________________________1___________1____________________________________________
____________________________________________1_________1_____________________________________________
_____________________________________________1_______1______________________________________________
______________________________________________1_____1_______________________________________________
_______________________________________________1___1________________________________________________
________________________________________________1_1_________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________

If anyone could give me a push in the correct direction, it would be greatly appreciated.

도움이 되었습니까?

해결책

the image is structured in the following way:

   Y01
   Y11
---------
   Y02
   Y12
---------
   Y04
   Y14
---------
   Y08
   Y18
---------
   Y016
   Y116

Y0x & Y1x for the letter Y: Y0x are the branches and Y1x is the trunc; x is the number if lines required to form the letter

E.g. Y02 & Y12 - 2 lines to form the branches and 2 line to form the trunk

1---1
 1-1
  1
  1 

You can notice that there is one more 'rule': when you start drawing the new letters (e.g. you go from Yx2 to Yx3) you copy the last line.

EDIT: Let's assume you have the drawing saved in a matrix char P[63][100] I would write 2 function (not the optimal approach but you can optimize it) - drawY - draws the Y letters according to inputs - drawPicutre - draws the "layers" of Y

drawY(int startPos, int stopPos, int*line, int lvl, int count)
{
   if(lvl>=count)
   {
     P[*line][(startPos+(stopPos-startPos))/2] = '1';
     P[*line + lvl][(startPos+(stopPos-startPos))/2 +count] = '1';
     P[*line + lvl][(startPos+(stopPos-startPos))/2 -count] = '1';
     (*line)--;
     drawY(startPos,stopPos,line,lvl,count+1);
   }

}

int line=63;
drawY(0,100, &line, 16, 1); // draws the first, and largest Y

You just need to implement the second function, drawPicture(...) which calls drawY. You can modify drawY if you want to.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top