Eight digital problems and their expansion

Source: Internet
Author: User

Let's start by introducing eight digital questions.

The game's chessboard is split into a 3x3 area with a square piece labeled with 1~8 eight numbers, and the remaining area is empty.

In the course of the game, you can only move pieces to adjacent empty areas. When little ho moves 8 pieces to the position shown, the game is over.

The question now is how to tell if the initial state can reach the target state?

For convenience, we write it in a one-dimensional string, with 0 instead of a space

302581647-->123456780

First, we introduce the concept of reverse order.

Set A to an ordered set of n numbers (n>1), where all the numbers are different.

If there is a positive integer i, J makes 1≤i < j≤n and a[i] > A[j], then <a[i], a[j]> this ordered pair is called a in reverse order, also known as the inverse number.

Find the sum of the inverse number of all numbers except 0, which is the sum of the number of numbers in front of each number that is larger than it, called the reverse of the state.

When the empty cell moves left and right in the same row, the reverse of the state does not change.

When the empty grid moves up and down in the same row, the number moves forward two or two, so there are three cases, two is larger or smaller than it, then the reverse order is reduced by two or plus two; two is a small one, then the reverse is reversed.

Therefore, the operation of the space does not change the parity of the sequence in reverse order, so long as the initial state and the target State in reverse order parity of the solution.

Target Status Reverse 0

302581647 in reverse order is 10, so there are solutions!

BOOLCheck () {ints[ -]; intCNT =0;  for(inti =0; i<3; i++){           for(intj =0; j<3; J + +) {s[3*I+J] =Start.map[i][j]; if(s[3*I+J] = ='x')                  Continue;  for(intK =3*i+j-1; k>=0; k--){                if(S[k] = ='x')                      Continue; if(s[k]>s[3*i+j]) CNT++; }          }      }      if(cnt%2)          return false; return true; }  
View Code

Let's expand on the eight digital questions, not the 3x3 squares, but the NXN squares.

In the same vein,

When the empty cell moves left and right in the same row, the reverse of the state does not change.

When the cell moves up and down in the middle, the number moves forward the N-1 bit or the N-1 bit.

Therefore, when N is an odd number, the parity of the reverse order does not change, so as long as the initial state and the parity of the target State are consistent with the solution.

And when n is even, because each move up and down, the parity of the reverse order is changed once, so to figure out the space to its target position required by the number of M,

Then it is determined that if the reverse number of the initial state plus M is the same as the reverse number parity of the target State, there is a solution;

Also try to think about nxnxn three-dimensional whether there is a solution, the principle should be similar (escape

Eight digital problems and their expansion

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.