[C ++ algorithm design] Eight Digital Problems

Source: Internet
Author: User

8. Digital question [question]: 1 ~ Place the eight square slide of 8 into three rows and three columns (a grid is empty). You can move the adjacent slide of the space to the space at a time, calculate the minimum number of steps that can move the target. If the target cannot be reached, the value is-1. [Analysis] We can define each situation as a "state", and each State is composed of nine lattice numbers in sequence, for example, the Left State is, 2, 4, 3, 0, 7, 8, 6, similarly, the right state is: 1, 2, 3, 4, 5, 6, 7, 8, 0. Then we can use the width-first traversal (BFS) method to expand a new state for each State, and then know that the target State is searched. [Code]

Typedef int State [9]; // defines the "State" type const int Maxstate = 1000000; State st [Maxstate], goal; // status array int dist [Maxstate]; // if you want to print the scheme, you also need to define a parent number array int fa [Maxstate]; const int dx [] = }; const int dy [] = {,-}; void init () {}// initialization function int bfs () {init (); int front = 1; int rear = 2; while (front <rear) {State & s = st [front]; if (memcmp (goal, s, sizeof (s) = 0) return front; int z; for (z = 0; z <9; z ++) if (! S [z]) break; int x = z/3, y = z % 3; for (int d = 0; d <4; d ++) {int newx = x + dx [d]; int newy = y + dy [d]; int newz = newx * 3 + newy; if (newx> = 0 & newx <3 & newy> = 0 & newy <3) {State & t = st [rear]; memcpy (& t, & s, sizeof (s); t [newz] = s [z]; t [z] = s [newz]; dist [rear] = dist [front] + 1; if (insert (rear) rear ++; // judge weight} front ++;} return 0 ;}

 

The above Code lacks a main () function, an init () function, and a judge-heavy insert () function. I think a more important point is to define the Statement of the "State" type, I started to think it was written like typedef int [9] State, but it was not correct. I don't know why it was written like that. It is easy to write errors. Then there is a lot of "references" in the most critical bfs function, which can be learned. This is very convenient and easier to understand than the pointer.

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.