1.2 Question of Chinese chess masters further discussion and expansion: how to use one variable to implement N-repeating? [Chinese chess]

Source: Internet
Author: User

[Question]

Assume that only two pieces of chess are left in Chinese chess. Chinese people all know the basic rules: the handsome guy can only move up or down the left and right sides, but cannot move in oblique directions. In this case, all possible positions will be handsome.Only one byte storage variable is required in the code..

[Analysis]

Three solutions:

1)Bitwise operationOne byte is used to store and read two variables.

2) UseBit domainSeveral different objects are represented by a byte binary field. For example

C ++ code
1
2
3
4
5
  Struct
{
Unsigned char A: 4;
Unsigned char B: 4;
} I;

3) use one variable to express a 2-cycle. This solution will be discussed later. (THINKING: how to use one variable to implement N-repeating ?)

Bitwise operation]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/24
*/
# Include <stdio. h>
# Define half_bits_length 4
# Define fullmask 255
# Define lmask (fullmask # Define rmask (fullmask> half_bits_length)
# Define rset (B, n) (B = (B & lmask) | (n ))
# Define lset (B, n) (B = (B & rmask) | (n) # Define rget (B) (B & rmask)
# Define lget (B) (B & lmask)> half_bits_length)
# Define gridw 3

Void solution1 ()
{
Unsigned char B;
For (lset (B, 1); lget (B) <= gridw * gridw; lset (B, (lget (B) + 1 )))
{
For (rset (B, 1); rget (B) <= gridw * gridw; rset (B, (rget (B) + 1 ))
{
If (lget (B) % gridw! = Rget (B) % gridw)
{
Printf ("A = % d, B = % d \ n", lget (B), rget (B ));
}
}
}
}

Bit domain]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/24
*/
Struct
{
Unsigned char A: 4;
Unsigned char B: 4;
} I;

Void solution2 ()
{
For (I. A = 1; I. A <= 9; I. A ++)
For (I. B = 1; I. B <= 9; I. B ++)
If (I. A % 3! = I. B % 3) // column not equal
Printf ("% d, % d \ n", I. A, I. B );
}

[Single Variable]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/24
*/
Void solution3 ()
{
Unsigned char I = 81;
While (I --)
{
// I = 9 * A + B, A = I/9, B = I % 9
If (I/9% 3 = I %9% 3)
Continue;
Printf ("% d, % d \ n", I/9, I % 9 );
}
}

We need to verify the 9*9 = 81 positional relationships between the "Jiang" and "Shuai" in our 3*3 lattice, which is also the origin of I = 81. In addition, we need to understand the meanings of I/9 and I % 9. We know that the integer I can be composed of two parts: I = (I/9) * 9 + I % 9. We note that in the process of I from 81 to 0, the change of I % 9 is equivalent to the inner loop, and the change of I/9 is relative to the outer loop.

[Expansion]

How to use one variable to implement N-repeating?

Let's take a look at a simple example. One Variable achieves a 2-repeating loop.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/24
*/
Void loopwith2variables ()
{
Unsigned char I, J;
For (I = 0; I <5; I ++)
For (j = 0; j <4; j ++)
Printf ("% d, % d", I, j );
}

Void loopwith1variable ()
{
Unsigned char val = 4*5;
While (Val --)
{
Printf ("% d, % d", (Val/4) % 5, Val % 4 );
}
}

[Summary]

For a * B = I, use the following formula:

Loop1 = I % B;

Loop2 = (I/B) %

Here, loop1 is the inner loop, and loop2 is the outer loop.

Then we can obtain the n-weight formula. Suppose an * a (n-1) * ...... * A3 * A2 * a1 = N

Loop1 = n % A1

Loop2 = (N/(A1) % A2

Loop3 = (N/(a1a2) % A3

.....

Loopn = (N/(a1a2... A (n-1) %

[Reference]

Http://blog.csdn.net/kabini/article/details/2256421

Http://blog.csdn.net/silenceburn/article/details/6133222

Http://blog.csdn.net/zhongkeli/article/details/8779168

Http://www.cnblogs.com/python27/archive/2012/04/10/2441114.html

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/chinese-chess.html

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.