java--Algorithm Special training (1) brute force cracking and practicality priority (Topic III)

Source: Internet
Author: User
Tags ming

Keywords:

Consider practicality first, without using the If for loop

Topic:

Xiao Ming recently taught the neighbors ' children elementary School Olympiad, and recently happened to tell the third-order magic side of this part.

Third Order magic Square refers to the 1~9 is not repeated into a 3*3 matrix, so that each row, each column and each diagonal is the same. Third-order magic Square is also known as Nine Gongge, in the primary School Olympiad has a very famous formula: "24 for the shoulder, 68 for the foot, left three right seven, Dai one, five in which", through such a formula can be very perfect to construct a nine Gongge. 4 9 23 5 78 1 6 Interestingly, all third-order magic squares can be obtained after a number of mirroring and rotation operations with such a nine Gongge. Now Xiao Ming is going to erase a third-order magic square (not necessarily the one in it) and give it to the neighbor's children to restore it, and hope she can determine whether there is only one solution. And you, also by Xiaoming delivered the same task, but the difference is that you need to write a program ~ input format: The input contains only a single set of test data. Each set of test data is a 3*3 matrix, where 0 of the part represents the part that was erased by Xiaoming. For 100% of data, satisfying the given matrix can at least restore a set of feasible third-order magic squares. Output format: If only one set of feasible third-order magic squares can be restored, output it, otherwise output "Too many" (without quotation marks). Sample Input 0 7 20 5 00 3 0 Sample Output 6 7 21 5 98 3 4



Solution Ideas:
1  PackageCom.algorithm.java.blueBirdge;2 3 //import static sun.misc.Version.println;4 5 ImportCom.sun.org.apache.bcel.internal.generic.RETURN;6 /*7 * 4 9 28 * 3 5 79 * 8 1 6Ten * */ One  A /* - * 0 7 2 - * 0 5 0 the * 0 3 0*/ -  Public classTESTSUANFA { -     //does every bit of the standard string match up with S? -     Static BooleanTest (String std,string s) { +          for(inti=0;i<std. Length (); i++){ -             if(Std.charat (i) ==s.charat (i))Continue;//if the match is up, look next, continue . +             if(S.charat (i) = = ' 0 ')Continue;//if the request string is 0, it can match any one of the standard strings A             return false; at         } -         return true; -     } -  -    Public Static voidMain (string[] args) { -  inString s= "072050030";//match with the required string -       //The form of all possible matrices is represented by a string toString[] SS ={//rotation produces 4 kinds, mirror produces 4 kinds, total 8 kinds +"492357816", -"834159672", the"618753294", *"276951438", $ Panax Notoginseng"294753618", -"438951276", the"816357492", +"672159834" A       }; the       //Match +        for(inti=0;i<ss.length;i++){ -           if(Test (ss[i],s)) { $               //The number in the string is still extracted and exported as a matrix $System.out.println (Ss[i].substring (0,3)); -System.out.println (Ss[i].substring (3,6)); -System.out.println (Ss[i].substring (6,9)); the  - Wuyi           } the  -       } Wu  -  About   } $  -  -  -}

java--Algorithm Special training (1) brute force cracking and practicality priority (Topic III)

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.