程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3087 Shuffle'm Up (模擬)

POJ 3087 Shuffle'm Up (模擬)

編輯:C++入門知識

POJ 3087 Shuffle'm Up (模擬)


Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K 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 performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

\

The single resultant stack, S12, contains 2 * C<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvZW0+IGNoaXBzLiBUaGUgYm90dG9tbW9zdCBjaGlwIG9mIDxzdHJvbmc+UzxzdWI+MTI8L3N1Yj48L3N0cm9uZz4gaXMgdGhlIGJvdHRvbW1vc3QgY2hpcCBmcm9tIDxzdHJvbmc+UzxzdWI+Mjwvc3ViPjwvc3Ryb25nPi4gT24gdG9wIG9mIHRoYXQgY2hpcCwgaXMgdGhlIGJvdHRvbW1vc3QKIGNoaXAgZnJvbSA8c3Ryb25nPlM8c3ViPjE8L3N1Yj48L3N0cm9uZz4uIFRoZSBpbnRlcmxlYXZpbmcgcHJvY2VzcyBjb250aW51ZXMgdGFraW5nIHRoZSAyPHN1cD5uZDwvc3VwPiBjaGlwIGZyb20gdGhlIGJvdHRvbSBvZiA8c3Ryb25nPlM8c3ViPjI8L3N1Yj48L3N0cm9uZz4gYW5kIHBsYWNpbmcgdGhhdCBvbiA8c3Ryb25nPlM8c3ViPjEyPC9zdWI+PC9zdHJvbmc+LCBmb2xsb3dlZCBieSB0aGUgMjxzdXA+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vcm1lZCBieSBzaHVmZmxpbmcgdHdvIHN0YWNrcyBzb21lIG51bWJlciBvZiB0aW1lcy48L3A+Cgo8cCBjbGFzcz0="pst">Input

The first line of input contains a single integer N, (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 integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1and S2 zero 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 though N), 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

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

Source

Greater New York 2006 一道模擬題, 解題思路:這個直接模擬(或者退化的BFS或DFS吧,就一種情況,汗。~)題目所要求的Shuffle的過程就行了,只是要注意搜索失敗的條件。當S1和S2又回到初始化狀態時就是搜索失敗的條件,為什麼會這樣呢?把所有的情況放在一個集合裡面,Shuffle會模擬到所有情況,直到回到初始狀態,無限循環下去。
代碼:
#include 
#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%s%s",&n,S1,S2,mat);
          x=1;
          while(1)
          {
             for(i=0;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved