Algorithm Learning (11), algorithm Learning

Source: Internet
Author: User

Algorithm Learning (11), algorithm Learning
1. King and Queen

Note: Game algorithm programming like chess has two main tasks:

1. Evaluate the location and check which parts can be taken;

2. Construct a very small algorithm to choose the location to move to the optimal value.

Let's first solve a simple problem: there is a chessboard with 8x8 squares. There are white kings and black queens. Check whether the queen can eat the king.

The Queen can move vertically, horizontally, or along any two diagonal lines to any distance.

8  - Q - - - - - -     - - - - - - - -7  - - - - Q - - -     - - - - - - Q -6  - - - - - - - -     - - - - - - - -5  - - - - - - - -     - - - Q - - - -4  - K - - - - Q -     - - Q - - - - -3  - - - - - - - -     - - - - - - - -2  - - - Q - - - -     - - - - - K - -1  - - - - - - - -     - Q - - - - - -   a b c d e f g h     a b c d e f g h

Let's take a look at these two examples and draw a board with a chart. In both cases, the king uses the letter K, and the letter Q shows the different positions of the Queen.

In the left-side chessboard, the King can be "eaten" by any of the four queens ";

On the right board, the king is safe-none of the four queens can eat him.

Input data: the first line contains the number of test cases.

The following line describes the location of the kings and queens of each test_case by specifying their squares (the kings are the first ).

Answer: each test contains a letter Y or N, which indicates whether the King can be eaten. Use spaces.

For example:

input data:8b4 b8b4 e7b4 d2b4 g4f2 b1f2 c4f2 d5f2 g7answer:Y Y Y Y N N N N

 

Test data:

26f4 e8c4 b6h2 h8h8 f8e8 d3a8 c6g7 h8d3 e2g1 a2e2 g5c7 d3f2 b3a6 e2e7 g4f7 c2b7 c1h3 b5e1 a7g4 b5e4 g6a3 h5b6 h7e3 h6a3 f1e7 e1g6 h6

 

The Code is as follows:

1 horizontally = ['A', 'B', 'C', 'D', 'E', 'F', 'G ', 'H'] # horizontal coordinate representation 2 3 test_cases = int (input () # Number of test cases 4 5 for I in range (test_cases): 6 data = input (). split () 7 K, Q = data [0], data [1] # obtain the coordinates of K and Q 8 if K [0] and Q [0] in horizontally: 9 if K [0] = Q [0] or K [1] = Q [1]: # When K and Q have the same horizontal and vertical coordinates, it is Y10 print ('y', end = '') 11 elif abs (horizontally. index (K [0])-horizontally. index (Q [0]) = abs (int (K [1])-int (Q [1]): 12 print ('y ', end = '') # When the difference between the horizontal and vertical coordinates of the two coordinates is equal, it is also Y13 else: # in other cases, it is N14 print ('n', end = '') 15 16 output: N Y

 

2. Cards Shuffling (Shuffling problem)

Note: shuffling is important for any type of card games. Because only a few programming languages have built-in functions to randomly process arrays (such as PHP), it is necessary to learn some useful algorithms.

Use the following symbols to describe cards:

ranks: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, Ksuits: C, D, H, S

CQ is "Plum Blossom Q", HT is "Peach 10", D2 is "square 2", and SA is "Black peach ".
There are a total of 52 cards, so they should be placed into an array of 52 elements before shuffling. The initial sequence of the array is as follows: 13 plum cards, 13 square cards, 13 red hearts cards, and the last 13 peach cards. The interior of each color card is arranged by Ace to King, so the entire deck looks like this:

[ CA, C2, C3, ..., CQ, CK, DA, D2, ..., DK, HA, H2, ..., HK, SA, S2, ..., SK ]

Then you should generate random numbers at 0 for each card. Exchange this card with another card to occupy the selected position (starting from 0 for the array index ).

FOR i = 0 ... 51 :    LET j = RANDOM(0 ... 51)    SWAP deck[i] WITH deck[j]

For example, we take the first card CA and generate a random value of 15 for it, which means that the card should be moved to location 15, where D3 is, and D3 should be moved back to location 0. Then we take C2 from position 1 and generate the next random number of 50, so C2 is exchanged with the other party. And so on.

You will get a sequence of non-negative integer random numbers-if they are greater than the necessary numbers, adjust them to the desired range and take the modulus of 52.

Input data: contains 52 non-negative integers. You should use them to shuffles as described.

Answer: cards that contain shuffling arrays should be separated by spaces.

For example:

input data:5814 1316 2080 2712 0 647 8098 315 44 6354 7867 100 61 763 6731 685 42 9309 569 92 701 562 85 8311 698 220 929 71 684 518 113 61 19 168 745 16 655 9548 6018 2686 25 785 81 721 964 85 44 614 4 509 8708 19answer:C5 D5 S4 C8 CQ S3 HK C9 H3 H6 D3 ST DT HT C6 CK DA H9 SJ SK DK C2 DQ S5 H4 D7 S7 S2 C4 D9 CT HJ HQ D2 SA CA H5 H2 C7 D4 CJ D6 S9 HA S8 D8 S6 SQ C3 DJ H8 H7

 

Test data:

0 537 320 6335 7581 140 4682 9944 2755 438 897 48 17 406 82 2368 1402 3179 524 3802 37 81 4993 68 6792 32 206 1300 61 43 950 244 44 550 5140 2434 4513 72 4007 726 51 66 8439 9450 956 872 363 100 679 436 48 739

 

The Code is as follows:

1 ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9 ', 'T', 'J', 'Q', 'K'] 2 suits = ['C', 'D', 'h ', 'S '] 3 start_deck = [] 4 for I in range (52 ): 5 suit = I // 13 6 rank = I % 13 7 card = suits [suit] + ranks [rank] 8 start_deck.append (card) # construct the initialization card group, sort 9 10 num = input () as required (). split () 11 nums = [] 12 for I in num: 13 nums. append (int (I) % 52) # obtain random positions 14 15 m = 016 for n in range (52): 17 start_deck [n], start_deck [nums [m] = start_deck [nums [m], start_deck [n] # assign a value to the card 18 m + = 119 20 for I in start_deck according to the rule exchange: # format the output card 21 print (I, end = '') 22 23 output: h2 CT C7 S9 D6 HJ D7 C8 SK D3 DA S8 C2 S4 H6 ht ck S3 C9 SJ H4 D5 SQ H7 DQ D4 ca dt C4 H8 C6 S6 H5 dk cj C5 HQ D9 HA s7 D2 H3 S5 D8 C3 S2 cq sa H9 HK ST

 

Related Article

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.