Algorithm for Solving a coin mobile game

Source: Internet
Author: User

I first saw this game in the glorious era of the 4 power enhanced edition (a very old single-host PC game. It is not so much a game as a smart question. The question is as follows: There are five coins, three front and two sides. They are arranged at intervals, as shown in 1. Figure 1 original arrangement of five coins each movement can only move two adjacent coins, that is, the two coins to be moved must be the front and the opposite, and the two coins are adjacent. It can be moved left or right, but there must be adjacent coins in that direction. Move across adjacent coins. For example, 1st and 2 Coins can be moved to the right, but cannot be moved to the left, because there are no adjacent coins on the left. Move to the right, as shown in figure 2. Figure 2 move the first two coins if they are not continuous, they can only be moved to the first place where the coins can be placed. For example, in the following example, to move the number of 3rd and 4th coins on the left to the left, you can only move them to the middle of the gap. You cannot skip the gap to the leftmost, as shown in 3. Figure 3 the final goal of another moving example is to move to one side and to the other side. Figure 4 the final goal is quite simple, but it is still quite difficult to do it. I tried it for a long time before I found the answer. However, this question is not difficult to solve by computer, and it is very suitable for solving with recursive algorithms. The following is the program I wrote. Because the calculation workload of this program is not large, I basically did not consider the computing efficiency, but made the program as simple and straightforward as possible. The program uses A string to store information about these coins. 'A' indicates the front, 'B' indicates the back, and space indicates the space. Therefore, the string is initialized to "ABABA ". To change the position of AB in the string, move the coin. The related coin moving operation is placed in the move function. The move function is passed in an integer parameter I, which is used to identify which two coins are currently being moved, move left or right. The specific method is that the lowest bit Of I represents the moving direction. In other words, when I is an even number, it indicates moving to the left. When I is an odd number, it indicates moving the coin to the right. The other bits of I indicate which two coins are being moved. The Return Value of the Move function indicates whether the Move is successful. The Code is as follows: [cpp] static bool move (string & coins, int I) {int dir = I % 2; // 0 indicates left shift, 1 indicates shift right I = I/2; if (coins [I] = ''| coins [I + 1] ='' | coins [I] = coins [I + 1]) {return false; // The current coin cannot be moved} if (dir = 0) // shift left {if (coins [I-1] = '') // No coin on the left, cannot move {return false;} else {string: size_type j = I-2; while (coins [j]! = ''& J> 1) {j --;} coins [j-1] = coins [I]; coins [j] = coins [I + 1]; coins [I] = ''; coins [I + 1] ='' ;}} else {if (coins [I + 2] = '') // No coin on the right, cannot move {return false;} else {string: size_type j = I + 3; while (coins [j]! = ''& J <coins. size ()-1) {j ++;} coins [j] = coins [I]; coins [j + 1] = coins [I + 1]; coins [I] = ''; coins [I + 1] ='' ;}} return true;} The isSeperated function determines whether coins are separated. The algorithm is simple, there must be more efficient algorithms. I don't have to worry about it, because for this small program, it must be much more time spent writing programs than running programs. Therefore, I am pursuing how to quickly write this program, rather than how to make it run faster. The following code: [cpp] static bool isSeperated (string coins) {int first = 0, last = coins. size ()-1; while (coins [first] = '') {first ++ ;} // locate the start position of these coins while (coins [last] = '') {last --;} // locate the Ending position of these coins if (coins [first] = coins [last]) return false; // The two coins at the beginning and end are the same, int I = first + 1, j = last-1; while (coins [I] = coins [first] | coins [I] = '') {I ++;} // find the first coin that is different from the first coin while (coins [j] = coins [last] | Coins [j] = '') {j --;} // find the last coin that is different from the last one if (I! = J + 1) return false; return true; // It indicates that the Solve function has been arranged here.} the Solve function is responsible for solving the problem. The algorithm is very simple. In short, it is an exhaustive method, if you try all possible moving methods, you will surely get the correct steps. The only thing to note is that the number of steps that can be moved is infinite. Therefore, you need to limit the search depth, that is, to limit the maximum number of steps to move, so that the endless lifting can come to an end. Searching by number of layers (Steps) is naturally the easiest way to write recursive algorithms. The following code: [cpp] bool solve (string coins, int depth) {string coins2 = coins; int first = 0; int last = coins. size ()-1; while (coins [first] = '') {first ++;} while (coins [last] ='') {last --;} for (int I = 2 * first; I <2 * (last-1); I ++) {if (move (coins, I )) {// indicates that you can move depth --; // The record moves a few steps if (isSeperated (coins) {//, and the output result is cout <coins <endl; return true;} else if (depth! = 0 & solve (coins, depth) // continue moving based on the current movement {cout <coins <endl; return true ;} else {// It indicates that the current movement is incorrect. coins = coins2; depth ++; continue; // test one kind of movement.} // It indicates that all the methods of movement have been tried. return false is not supported.} Finally, the main program is used, just a few lines, needless to explain: [cpp] int main () {string coins ("ABABA"); if (solve (coins, 5) {cout <coins <endl ;} return 0;} the output result of the program is as follows: aaabb abaa B A ABAB AABAB ABAAB ABABA must note that because it is recursive, the output result should be viewed from the bottom up.

Related Article

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.