uva297 (Quadtree)

Source: Internet
Author: User

Give us two quadtree of the pre-sequence traversal, want me to ask the original 32*32 matrix how many positions are black

Quardtree is to divide the area into equal 4 sub-spaces, and then recursively divide the 4 sub-spaces, knowing that the termination is broken after the condition is met

The termination condition of this problem is that it encounters a black or white node that stops recursion.

As for why a pre-order traversal can make a contribution, it is because of the color of the knot, so it is possible to know when to return recursively. So I can build a lesson tree

There is nothing to play Java code, anyway, C + + syntax are very familiar with the Java brush problems, brush questions, learn Java two not mistaken

1 ImportJava.util.Scanner;2 3 4  Public classMain {5     StaticScanner cin =NewScanner (system.in);6     Final Static intLEN = 32;7     Static intAns = 0;8     StaticString str;9     Static intp;Ten     Static int[] Mat =New int[Len][len]; One      Public Static voidDfsintRintCintW) {//(R,C) is the upper left corner A          -         Charch = Str.charat (p++); -         if(ch== ' P '){ theDFS (R,C+W/2,W/2);//for section 1 in the diagram, -DFS (R,C,W/2);//for section 2 in the diagram, -DFS (R+W/2,C,W/2);//for section 3 in the diagram, -DFS (R+W/2,C+W/2,W/2);//for area 4 in the diagram, +         } -         Else if(ch== ' F ') {//fills a rectangle if it encounters a black node . +              for(intk=0; i<r+w; ++i) A                  for(intJ=c; j<c+w; ++j) at                 if(mat[i][j]==0) {//because it is a combination of two trees, it is possible to repeat the fill, so only when the padding is 0 ans++ -MAT[I][J] = 1; -ans++; -                 } -         } -     } in      Public Static voidinit () { -Ans = 0; to          for(inti=0; i<len; ++i) +              for(intj=0; j<len; ++j) -MAT[I][J] = 0; the     } *      Public Static voidMain (string[] args) { $         intN;Panax Notoginseng          -n =cin.nextint (); the cin.nextline (); +          for(inti=0; i<n; ++i) { A init (); thep = 0; +str =cin.nextline (); -DFS (0,0, LEN); $p = 0; $str =cin.nextline (); -DFS (0,0, LEN); -System.out.println ("There is" +ans+ "black pixels."); the         } -     }Wuyi  the}
View Code

uva297 (Quadtree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.