"The fun of Arithmetic"--Huarong road game

Source: Internet
Author: User
Tags arithmetic

This chapter is a brief introduction to the Huarong game and how to use the algorithm to calculate the optimal number of steps.

First of all, for the Huarong game, let's introduce its rules.

Sort of like a jigsaw puzzle, essentially this is a 5x4 matrix, and our goal is to get the matrix of the Caocao (2x2) out of the 5th row of the 5x4 matrix from the 3 and 4 columns.

The rules of the game are simple, but it is not so easy to do it with the smallest number of steps, which is the problem we are going to focus on.

In fact, for the readers who have studied DFS and BFS, may have thought of the solution of the idea, in fact, can search all the state, and then form a state tree (this and the author described in the game of the two games tree is a thing), in the process of achievement, we set the parameter step to record a node (i.e. a game state) Steps, you can easily find the minimum steps after you have traversed all the chess states.

Let's try to refine the data structure of this process. First and foremost, in the state tree each node records the different chess states. It is easy to see that there is a state of chess (we can use a two-dimensional array to store) and need the parameter step to record the number of steps. Do you need some other data?

We can see that in the process of building a state tree, a key is to establish a parent-child node connection, so for a node, we also want to store its parent node state, which establishes the link between the node and the previous tree structure, and we also have parameters to record based on the layout of the node, the next step of the move, That is, a leader in the next layer of the state tree is generated.

From this we can write a structure to record a state.

struct hrd_game_state{   char board[high][wide];  // Chess status   Move_action move;       // the next move strategy   int step;                          // move number of steps   // parent Node }

Easy to see in the analysis above, our description of the "Next steps to move" is too vague, and in programming we need to quantify each action into a single visual piece of information. For the "Move Step", first we need to know who to move, this is a parameter, and the second we need to know where to move, this is another parameter. So the move in the data structure above is actually a struct variable. In the form of the next.

struct tagmove_action{   int  heroinx;    int Diridx;} move_action;

Similarly, we carry out top-down programming, we have a choice of four directions for the direction in "every step", so we need to continue to construct an array of structs to implement the description of the variable DIRIDX to four direction one by one.

struct tagdirection{  int  HD;   int VD;} DIRECTION;  DIRECTION directions[4] = {{0,1}, {0,-1},{1, 0},{-1,0}};// here is the matrix coordinate, moving up and down the row column unchanged, left and right move column edge row unchanged. 

Preliminary understanding of the solution to solve the Huarong algorithm data structure, in fact, and game tree very similar, the following is more important pruning and optimization.

< not finished >

"The fun of Arithmetic"--Huarong road game

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.