Hex Magic Square-solving

Source: Internet
Author: User

Title: Hex Magic Square

Put 1 2 3 ... 19 A total of 19 integers are arranged in six corner shapes, as follows:


* * *
* * * *
* * * * *
* * * *
* * *


Requires that the sum of the numbers on each line must be equal. A total of 15 lines Oh!


Give me some more clues! We filled out 2 numbers in advance, the first two numbers are: 15 13, see figure "P1.png", yellow a behavior is asked.


Please fill out the 5 numbers in the middle row. The numbers are separated by spaces.

This is a line separated by a space of integers, please submit the answer through the browser, do not fill in any unnecessary content (such as descriptive text, etc.)


Problem-Solving ideas: DFS


Source:

/** * Hex Magic Side Search * * */public class main1{static int table[] = {1,2,3,4,5,6,7,8,9,10,11,12,14,16,17,18,19};//Content Public St atic void Main (string[] args) {int arr[] = new Int[17];boolean step[]= new boolean[20];step[15] = step[13] = True;dfs (arr, 0, step);} private static void Dfs (int[] arr, int ws, boolean[] step) {if (ws = = 5) {if (+ arr[0]! = arr[1] + arr[2] + arr[3] + arr[ 4]) return;} if (ws = = 6) {if (+ arr[0]! = + Arr[1] + arr[5]) return;    if (ws = =) {if (+ arr[0]! = Arr[5] + arr[6] + arr[7] + arr[8] + arr[9]) return; if (+ arr[0]! = Arr[0] + arr[4] + arr[9]) return; if (ws = =) {if (+ arr[0]! = + arr[2] + arr[6] + arr[10]) return; if (ws = =) {if (+ arr[0]! = arr[10] + arr[11] + arr[12] + arr[13]) return;if (+ arr[0]! = + Arr[3] + arr[8] + ar R[13]) return; if (ws = =) {if (+ arr[0]! = Arr[5] + arr[10] + arr[14]) return;if (+ arr[0]! = Arr[0] + arr[3] + arr[7] + arr[11]+ ARR[14]) return; if (ws = =) {if (+ arr[0]! = arr[14] + arr[15]+arr[16]) reTurn;if (+ arr[0]!=15+ arr[2]+arr[7] + arr[12]+arr[16]) return;if (+ arr[0]! = arr[9] + arr[13] + arr[16]) Return;o Utput (arr); return;}  for (int i = 0; i < table.length; i++) {if (Step[table[i]] = False) {Step[table[i]] = true; Arr[ws] = Table[i];d FS (arr, Ws+1, step); Step[table[i]] = false;} }}private static void Output (int[] arr) {for (int i = 0; i < arr.length; i++) {System.out.prin         T (Arr[i] + ""); } System.out.println ();}}


Hex Magic Square-solving

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.