Reverse number-puzzle game essential knowledge

Source: Internet
Author: User
Tags benchmark

Nearly two days to prepare a puzzle game tutorial, ready to encounter some problems, collect and save to share down, one's own use, and secondly up a bit of knowledge, Sunline have the need of small partners just also look.

The cause is simple, such as the following puzzle (matrix):

1 2

3 Empty

Such a 2*2 matrix is the standard primitive matrix, but it changes:

3 1

2 Empty

This is reversible (empty and nearby can be swapped position, empty 2, empty 3, empty 1, empty 2 can).

But what about the following?

1 3

2 Empty

Can you still restore it?

This is not to blame you, is really unable to restore.

At that time in the brain after a bit, found that no, and then check the information, really have predecessors have been related to the problem, and have a reliable answer.

If two matrices, from left to right, from top to bottom, two of the inverse number of the Matrix + empty row + Empty The value of the column number of the same parity, you can restore or equivalent; If different, it cannot be restored.

Again above the question, tried to play a few jigsaw puzzles, found that these guys are chicken thieves, each block can be directly clicked and any position of the interchange, then there is no solution to the problem ah ...

The problem is, the game itself is almost out of difficulty, the game can not be seen ...

Oh, yes, forget to say what is "reverse number", the reverse number is interpreted as follows:

For example, the 1234 alignment is the benchmark, and then the two sorts are 4321, 3214, respectively.

The reverse she used is based on the original sort, and the order of the new permutation is different from the base sort order.

So the analysis that 4321. 43, 42, 413, 32, 312, 211, so the reverse number is 1 + 2 + 3 = 6, while 3214,31, 322, 211, so the reverse number is 2 + 1 = 3. 1234 is a benchmark, or 0. So 4321 and 1234 are equivalent, solvable, 3214 and 1234 are not equal, and are not solvable.

The top comparison, 123 is the benchmark, 312 reverse order number is 2,132 reverse order number is 1, so 132 is not solvable.

The calculation is very complex, but through the double-cycle to deal with, as long as not too many numbers are also acceptable, after all, is to play games, even if the 10*10 matrix, the time complexity of the sky is also o^2, optimization into Olgo is not no, so still can engage.

The specific algorithm code is as follows:

// 基准就是自然排序,递增是正常的,就是后面一定比前面的都大。const cal = num => {  let arr  if(typeof num === ‘number‘) {    let strs = String(num)    arr = Array.from(strs, str => Number(str) || 0) // 安全处理,转换错误给0  } else arr = num  let length = arr.length  let reverse = 0  for(let i = 0; i < length - 1; i++) {    let n = arr[i]    for(let j = i + 1; j < length; j++) {      let m = arr[j]      if(n > m) reverse += 1    }  }  console.log(reverse)}cal(4321) // 6cal(3214) // 3cal(312) // 2cal(132) // 1cal(54321) // 10cal([10, 14, 21, 7, 3, 12, 0]) // 15

This is good, the next time to write jigsaw puzzles, as long as to focus on business logic, the game initialization of the problem with this code expansion can be.

There is also a way to initialize the time according to the standard sequence to simulate the legal movement N times, and then reverse engineering can be. But the scheme is almost 0 more scalable, so give up.

Write business to pay attention to this kind of trap logic ha, wish you have fun.

Like the article, please pay attention to a wave, regular more technical articles, full of dry goods.

Reverse number-puzzle game essential knowledge

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.