Js implements the automatic solution program for cup pouring Problems

Source: Internet
Author: User

Similar logic problems are often encountered in the intelligence test questions. A few cups of different capacities will let you pour out much water.

On Android, a game dedicated to playing this question is called Water Logic.


I installed this game to repeat dozens of off-board cartoons. I felt that the level design of this game was very poor, and the difficulty of the level was not increasing progressively. There were a lot of lower-end levels, and lack of difficult levels.

As a programmer, we should be able to play with this type of questions. We can do it easily in 10 or more steps, but it may not be able to easily reach the minimum number of steps.

There are three star obsessive-compulsive gamers and programmers who write such a small program that automatically solves the problem. This problem will no longer be a problem in the future.


Click here to try to solve the problem of cup pouring water automatically

Basic algorithm logic:

Each Cup can be full, empty, or poured into other cups, so the total number of cups * (number of cups-1 + 2)

For three cups, there are 12 types of operations available in each step. If there are two cups, there are 6 types of operations available in each step.

Traverse each operation, record the amount of water in each cup after the operation is completed, and calculate a key for creating a map.

When traversing various reverse operations, if the key already exists and the number of current steps is greater than the number of previously recorded steps, this operation is discarded.

This small program can only solve the problem of pouring water from two or three cups. It is not written into N cups, and the code has a lot of hard code.

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.