Monk Wits-The puzzle

Source: Internet
Author: User

Title: Monk Wits


Ancient funeral activities often asked the monks to practice things. At the end of the ceremony, there are sometimes "monk wits" fun programs to relieve the oppressive atmosphere.


The program is in the following steps: First use grain (usually rice) on the ground "painting" a number of steps (indicating the N-level pagoda). There are also a number of small monks randomly "stand" on a certain step. The highest level of stairs must stand people, others arbitrarily. (as shown in 1)


The two masters of the game commanded a small monk to go up the steps of any level, but will be standing on the high steps of the little monk block, can not be crossed. Neither of the two young monks can stand on the same steps, nor move to the lower steps.


The two wizards take turns issuing instructions, and eventually all the young monks will be squeezed in high steps, and can no longer move upward. When it was the master's turn to be unable to move on, the master conceded that the game was over.


For the known number of steps and the location of the small monk, please calculate how the first-order mage should make the decision to win.


The input data is a row of n integers separated by spaces, indicating the location of the little monk. The step number is counted from 1, so the last little monk's position is the total number of steps. (n<100, number of steps <1000)

The output is a line of two integers separated by a space: a B, which means to move the small monk in position a to position B. If there is more than one solution, the output of a small solution of a value, if no solution is output-1.




For example:
User input:
1 5 9
The program output:
1 4


Again such as:
User input:
1 5 8 10
The program output:
1 3


Resource contract:
Peak memory Consumption < 64M
CPU Consumption < 1000ms

Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Redundant content.


All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.


Note: The main function needs to return 0
Note: Use only ANSI c/ansi C + + standards, and do not invoke special functions that depend on the compilation environment or operating system.
Note: All dependent functions must explicitly #include <xxx&gt in the source file, and the common header files cannot be omitted from the project settings.

When committing, be careful to choose the type of compiler you expect (never confuse C and CPP).




Import java.util.scanner;/** * * Monk wits * * Problem solving ideas: can be converted to the Nim game method. * * * * TEST data: * 1 5 8 19 22 34 56 78 100 * 34 51 * 56 75 * * * 1 2 0 4 0 * 1 3 6 7 12 13 * 0 1 2 3 * */public class Asist{public static void Main (string[] args) {Scanner sc = new Scanner (system.in); String []SS = Sc.nextline (). Split (""); int a[] = new int [Ss.length];for (int i = 0; i < a.length; i++) A[i] = Integer.parseint (ss[i]);//temporary array--the number of lattice between monks int b[] = new int [A.length];int B1 = 0;for (int i = 1; i < A.lengt H i++) {b[b1++] = A[i]-a[i-1]-1;}  Convert to nim solve int sum = 0;    for (int i = 0; I <= B1; i+=2) {sum ^= b[i];}    if (sum = = 0) System.out.println ("-1");    else {//the movement of all monks, brute force for (int i = 0; i < a.length; i++) {for (int j = 1; J <= B[i]; j + +) {    B[i]-=j;        if (i!=0) b[i-1] + = j;    sum = b[0];    for (int k = 2; k <= B1; k+=2) {sum ^= b[k];    } if (sum = = 0) {System.out.println (a[i]+ "" + (A[I]+J));   Break }//Backtracking b[i] + = j;    if (i!=0) b[i-1]-= j; }    }    }}}


Monk Wits-The puzzle

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.