2015-08-18 23 views
1

我正在構建一個智力遊戲,很難解釋,所以我舉了一個例子。在不同列表中巧妙地構建對元素

我有一個單詞列表(也可以是無窮大):

String myList[] = {"chair", "house", "ocean", "plane", "dog", "TV", "grass", "money" etc....} 

現在棘手的問題,我需要建立對指數/字4個列表(每個列表都有相同的大小),隨機,但符合以下規則: 如果我選擇了一個數字,與此數字匹配的字詞僅出現在2個列表中。

例如,這將是正確的:

List1: 
1/chair 
2/house 
3/plane 
4/grass 

List2 
1/chair 
2/dog 
3/plane 
4/TV 

List3: 
1/ocean 
2/house 
3/money 
4/TV 

List4 
1/ocean 
2/dog 
3/money 
4/grass 

例如:

如果我選擇3號,然後列出3和表4匹配字「錢」,清單1和2匹配單詞'飛機'。總是必須有兩個匹配的列表(永遠不會少,從不多)。他們應該從大量的單詞中隨機構建,因此當您選擇一個數字時,您無法猜測哪個列表會匹配。

我試圖用一個很好的簡單遞歸算法做到這一點。但我失敗了。

+1

你能分享你的嘗試和失敗的原因的原因。這將更加符合本網站的精神。 –

+0

數組不能是無限的。 –

+0

好的讓我們用infinit代替巨大的... – Tyvain

回答

0

像這樣的東西應該做的伎倆(你可以改變提供單詞和相應的LISTSIZE相應)。單詞的數量應該可以通過listSize來分割,以填充所有列表。

public static void main(String[] args) { 
    String[] words = new String[] { "chair", "house", "ocean", "plane", 
      "dog", "TV", "grass", "money" }; 
    // valid list sizes for 8 words: 1, 2, 4, 8 
    int listSize = 4; 
    List<String[]> result = distributeRandomly(words, listSize); 
    for (String[] resultList : result) { 
     for (int index = 0; index < listSize; index++) { 
      System.out.println((index + 1) + "/" + resultList[index]); 
     } 
     System.out.println(); 
    } 
} 

private static List<String[]> distributeRandomly(String[] words, int listSize) { 
    // each word goes into 2 lists, so how many lists do we need? 
    int listCount = words.length * 2/listSize; 
    if (listCount * listSize != words.length * 2) { 
     throw new IllegalArgumentException("Number of words" 
       + " must be a multiple of the size of the individual lists!"); 
    } 
    // initialize result lists (here arrays) in fitting size 
    List<String[]> listsToFill = new ArrayList<String[]>(listCount); 
    for (int index = 0; index < listCount; index++) { 
     listsToFill.add(new String[listSize]); 
    } 
    // be sure to randomly pick the given words by shuffling them 
    List<String> shuffledWords = new ArrayList<String>(Arrays.asList(words)); 
    Collections.shuffle(shuffledWords); 

    List<String[]> result = new ArrayList<String[]>(listCount); 
    int maxWordPosition = listSize - 1; 
    // distribute words 
    for (String word : shuffledWords) { 
     // word is supposed to be inserted in two lists at the same index 
     int wordPosition = -1; 
     // iterate result lists 
     Iterator<String[]> listIterator = listsToFill.iterator(); 
     while (listIterator.hasNext()) { 
      String[] list = listIterator.next(); 
      if (wordPosition == -1) { 
       // look out for the first list with an empty slot 
       for (int index = 0; index < listSize; index++) { 
        if (list[index] == null) { 
         // found empty slot at this index 
         wordPosition = index; 
         // insert word here (first list) 
         list[wordPosition] = word; 
         if (wordPosition == maxWordPosition) { 
          // the list is full, no more empty slots here 
          listIterator.remove(); 
          result.add(list); 
         } 
         break; 
        } 
       } 
      } else if (list[wordPosition] == null) { 
       // found second list with an empty slot at the same index 
       list[wordPosition] = word; 
       if (wordPosition == maxWordPosition) { 
        // the list is full, no more empty slots here 
        listIterator.remove(); 
        result.add(list); 
       } 
       // we are done with this word 
       break; 
      } 
     } 
     // shuffle result lists again, to ensure randomness 
     Collections.shuffle(listsToFill); 
    } 
    return result; 
} 

這將產生(例如)以下的輸出:

1/grass 
2/TV 
3/plane 
4/ocean 

1/grass 
2/dog 
3/money 
4/chair 

1/house 
2/dog 
3/money 
4/ocean 

1/house 
2/TV 
3/plane 
4/chair 
1

我對這個問題初始方法是

  1. 選擇在宇宙中的一個隨機單詞
  2. 分配選定字兩個列出了
    • 是不同的,
    • 不全部
  3. 將隨機詞存儲在一組關閉萬一的話,再次選擇
  4. 沖洗和重複,直到所有的名單都充滿
+0

這似乎很容易...我今晚嘗試這個,看看它是否工作。 – Tyvain

相關問題