POJ 3087 Shuffle & amp; #39; m Up (simulation)

Source: Internet
Author: User

POJ 3087 Shuffle & #39; m Up (simulation)
Shuffle'm Up

Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:5850 Accepted:2744

Description

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is already med by starting with two stacks of poker chips,S1AndS2, Each stack containingCChips. Each stack may contain chips of several different colors.

The actual shuffle operation is already med by interleaving a chip fromS1With a chip fromS2As shown belowC= 5:

The single resultant stack,S12, Contains 2 *C <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Region + region + uzxzdiffusion + region + serial + PC9zdHJvbmc + Serial + PC9zdHJvbmc + l1_vcd4kpha + hybrid + uzxzdiffusion + Serial + Cgo8cCBjbGFzcz0 = "pst"> Input

The first line of input contains a single integerN, (1 ≤N≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integerC, (1 ≤C≤ 100) which is the number of chips in each initial stack (S1AndS2). The second line of each dataset specifies the colors of each ofCChips in stackS1, Starting with the bottommost chip. The third line of each dataset specifies the colors of each ofCChips in stackS2Starting with the bottommost chip. Colors are expressed as a single uppercase letter (AThroughH). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 *CUppercase letters (AThroughH), Representing the colors of the desired result of the shufflingS1AndS2Zero or more times. The bottommost chip's color is specified first.

Output

Output for each dataset consists of a single line that displays the dataset number (1 thoughN), A space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. if the desired result can not be reached using the input for the dataset, display the value negative 1 (? 1) For the number of shuffle operations.

Sample Input

24AHAHHAHAHHAAAAHH3CDECDEEEDDCC

Sample Output

1 22 -1

Source

Greater New York 2006 is a simulated question. The solution is like directly simulating (or degrading BFS or DFS, it's just a situation, Khan .~) The Shuffle process required by the question is enough, but you must pay attention to the condition that the search fails. When S1 and S2 return to the initialization status, the search fails. Why? Place all the situations in a set, and Shuffle will simulate all the situations until it returns to the initial state and repeats infinitely.
Code:
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
Using namespace std; # define M 110 char mat [220], S1 [M], S2 [M]; char map [1000] [220]; int main () {int n, m, I, j, x, k; while (scanf ("% d", & m )! = EOF & m) {for (k = 1; k <= m; k ++) {memset (map, '0', sizeof (map )); memset (mat, '0', sizeof (mat); scanf ("% d % s", & n, S1, S2, mat ); x = 1; while (1) {for (I = 0; I
       
        

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.