Algorithmic Learning (11)

Source: Internet
Author: User
Tags shuffle

1.King and Queen

Description: Game algorithmic programming such as chess has two main tasks:

1. Assess the location and check which parts can go;

2. Construct a very small algorithm to select the position to move to the optimal value.

Let us first solve a simple problem: there is a chessboard, there are 8 x 8 squares. It has a white king and a black queen. Check if 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-6     ---------------------to----  -5------------------     4  -K-----------------------------     3  -     ------------ --2  ---Q--          ----------------------------------------------------- A b c d e F g h

Take a look at these two examples, draw the board with a chart. In both cases, the king is denoted by the letter K, while the letter Q shows the Queen's different positions.

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

In the right chessboard, the king is in a safe position-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 each king and queen of the test_case by specifying their squares (the king is the first one).

Answer: Each test gives a letter Y or N, which indicates whether the king can be eaten. With a space interval.

For example:

input data:8b4 b8b4 e7b4 d2b4 g4f2 b1f2 c4f2 d5f2 g7answer:y y y y 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 h6a 3 F1e7 e1g6 h6

The code is as follows:

1horizontally = ['a','b','C','D','e','F','g','h']#horizontal representation of coordinates2 3test_cases = Int (input ())#number of test cases4 5  forIinchRange (test_cases):6data =input (). Split ()7K, Q = Data[0], data[1]#get the coordinates of K,Q8     ifK[0] andQ[0]inchHorizontally:9         ifK[0] = = Q[0]orK[1] = = Q[1]:#when the k,q horizontal ordinate has one of the same, then yTen             Print('Y', end=' ') One         elifABS (Horizontally.index (k[0))-Horizontally.index (q[0]) = = ABS (int (k[1))-Int (q[1])): A             Print('Y', end=' ')#when the difference of the horizontal ordinate of the two coordinate is equal, also y -         Else:#other conditions are n -             Print('N', end=' ') the  -Output: n n y y n y y x y n n n-n y n n n n n-n y n n y N y y

2.Cards Shuffling (Shuffle problem)

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

Use the following symbols to describe the card:

Ranks:a, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, Ksuits:c, D, H, S

CQ is "Plum Q", HT is "Heart Peach", D2 is "Block 2", SA is "spades a".
There are 52 cards in total, so before shuffling, they should be placed in an array of 52 elements. The initial order of the array is as follows: 13 clubs, 13 squares, 13 hearts, and the last 13 spades. The interior of each suit card is arranged by Ace to King, so the whole deck looks like this:

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

Then you should generate some random numbers for each card at 0. Swap this card with another card to occupy the selected position (let the index of the array start at 0).

For i = 0 ... Wuyi :    = RANDOM (0 ... )    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 the location 15,d3 there, D3 should be moved back to position 0. Then we take the C2 out of position 1 and generate the next random number 50, so C2 and squared are exchanged. Wait a minute.

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

Input data: Will contain 52 non-negative integers, you should use them to shuffle as described.

Answer: Should contain the shuffle array of cards, and spaces separate.

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 929 684 518 113 (168 745) 655 9548 6018 2686 785 Bayi 721 964 4 614 509 8708 8 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 9 50 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:

1ranks = ['A','2','3','4','5','6','7','8','9','T','J','Q','K']2suits = ['C','D','H','S']3Start_deck = []  4  forIinchRange (52):5suit = I//136Rank = i% 137Card = Suits[suit] +Ranks[rank]8Start_deck.append (Card)#construct the initial set of cards and arrange them as required.9 Tennum =input (). Split () OneNums = [] A  forIinchNum: -Nums.append (int (i)% 52)#get a random number of positions -  them =0 -  forNinchRange (52): -Start_deck[n], start_deck[nums[m]] = Start_deck[nums[m]], Start_deck[n]#Exchange Assignment cards by rules -M + = 1 +  -  forIinchStart_deck:#Format output Cards +     Print(I, end=' ') A  atOutput: H2 CT C7 S9 D6 HJ D7 C8 SK D3 DA S8 C2 S4 H6 HT DJ 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

Algorithmic Learning (11)

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.