2014-03-27 79 views
3

好吧,這是我的任務。我在概念上理解需要做什麼,但是我在執行它的過程中遇到了麻煩。2D字符數組遞歸搜索最大組

收件它接受的文件名(作爲一個「命令參數」)爲隨機字母的二維網格和返回名稱和A的基團,B等的基團的數目的程序(附件是正如所寫的,文中7.46部分解決方案的一個例子。)

您可以假設網格正好是10行和20列。 最多可以有3個獨特的字符(並不是算法的上限)。 注意:在處理開始之前,您不需要知道實際存在多少個唯一字符,但假設「上限」只是讓您的代碼能夠爲給定數量「準備」。

電網不「包裹」(即從左側移開並不會將您移回到右側,如Pac-Man) 如果您發現電網更容易環繞環面(頂部連接底部/左側連接到右側),隨時可以這樣做。只是表明你在提交時做出了這個設計決定。 圍繞圓環包圍網格實際上使得遞歸代碼更小一點,但它在概念上更困難一點。

組是水平或垂直連接的匹配字母的集合。 對角元素未連接。 您的程序應顯示:

每個字母的唯一分組總數。 最大組的字母(和相應的大小)。 實例:

輸入文件:

BBBBBABAAAAABAABBBAA 
AAAABBABBABBBABAABBA 
AAABABABAABBBBBABBAB 
BBAAAABAABBBBAABBBAB 
BAAABAABAAABBBAAAABA 
AABABBAAABBBABBBAABA 
BABBAAAABABBBBBAAABB 
BABABAABAAAABAABBBAA 
BABBAAAABBBABBAAAABB 
ABABBBBBABAAABABAAAA 

最大組是A與起始於(0,7) 組計數49個成員:A = 17組,B = 22組

BBBBBABXXXXXBAABBBAA 
XXXXBBABBXBBBABAABBA 
XXXBABABXXBBBBBABBAB 
BBXXXXBXXBBBBAABBBAB 
BXXXBXXBXXXBBBAAAABA 
XXBABBXXXBBBABBBAABA 
BXBBXXXXBABBBBBAAABB 
BXBABXXBAAAABAABBBAA 
BXBBXXXXBBBABBAAAABB 
ABABBBBBABAAABABAAAA 

這就是在49(我是個把A看作是組成49的X's)。

這是我到目前爲止有:

public static void main(String[] args) throws FileNotFoundException { 
    args = new String[]{"this is where the file path goes"}; 
    final File inputFile = new File(args[0]); 
    final Scanner input = new Scanner(inputFile); 
    char[][] grid = new char[10][20]; 

    //Creates the 2d array 
    for (int row = 0; row < 10; row++) { 
     String c = input.nextLine(); 
     for(int col = 0; col < 20; col++){ 
      grid[row][col] = c.charAt(col); 
     } 
    }  

    display(grid); 
    search(grid);   
} 

// method to display the input file. Assuming no grid is no more than 10 rows 
private static void display(char[][] grid){ 
    for (int i = 0; i < 10; i++){ 
     for (int j = 0; j < 20; j++) { 
      System.out.print(grid[i][j]); 
     } 
     System.out.println(); 
    } 
    System.out.println(); 
} 

private static void search(char[][] grid) { 
    int x = 0; 
    int y = 0; 

    checkRight(); 
    checkBelow(); 
} 

我真的不知道該怎麼傳遞給我的遞歸算法或如何跟蹤的獨特角色。這裏是我通過的文件

CCCCACCBCCAACABBBBCA 
AABCABCBABCBBBACBBCB 
ABACACCAABCBCBBBCBAC 
ABABCCCBAAACBBABBCCC 
BABAAABCCAAACABACAAB 
BBCCBCACBCBACABAACBB 
BCCBCBCCCAABACCCCCBB 
ABBBBCCBAACCABCBCBAB 
BCAACCBCBACAACBABCCB 
BCBAABCACAABABBBAABA 

任何幫助將不勝感激,謝謝!

+0

在示例中,您如何獲得49個輸入文件的成員?你能突出這些嗎? –

+0

@McKevin在那裏你去 –

+0

那裏你去... –

回答

2
package recursivesearch; 

import java.awt.Point; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Map.Entry; 
import java.util.Scanner; 


public class Recursion { 

    public static ArrayList<Point> searched = new ArrayList<Point>(); 
    public static ArrayList<Point> groupOrigins = new ArrayList<Point>(); 
    public static int groups = 0; 
    public static boolean finished = false; 
    public static ArrayList<Integer> numbers = new ArrayList<Integer>(); 
    public static HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
    public static HashMap<Character, Integer> groupCount = new HashMap<Character, Integer>(); 
    public static char largestChar; 
    public static int largestInt = 0; 
    public static int largestIndex = 0; 

    public static void main(String[] args) throws FileNotFoundException { 
     final File inputFile = new File(args[0]); 
     final Scanner input = new Scanner(inputFile); 
     char[][] grid = new char[10][20]; 

     // Creates the 2d array 
     for (int row = 0; row < 10; row++) { 
      String c = input.nextLine(); 
      for (int col = 0; col < 20; col++) { 
       grid[row][col] = c.charAt(col); 
      } 
     } 
     numbers.add(0); 
     display(grid); 
     search(grid); 
     // System.out.println("NUMBER SIZE"+numbers.size()); 
     for (int i = 0; i < numbers.size(); i++) { 
      // System.out.println("NUmber is "+numbers.get(i)); 
      if (numbers.get(i) > largestInt) { 
       largestInt = numbers.get(i); 
       largestIndex = i; 
      } 
     } 
     // System.out.println("INDEX IS "+ largestIndex); 
     // System.out.println("Groups" + groups); 
     // for(Point i :searched) 
     // { 
     // System.out.println("Searched" +i.toString()); 
     // } 

     // System.out.println(map.toString()); 
     getLargest(); 
     /* 
     * for(int i = 0;i<groupOrigins.size();i++) { 
     * System.out.println("Group origin for group "+i +": "+ 
     * groupOrigins.get(i)); } 
     */ 
     System.out.print("Largest Group is " + largestChar + " with " 
       + largestInt + " members starting at (" 
       + (int) groupOrigins.get(largestIndex).getX() + "," 
       + (int) groupOrigins.get(largestIndex).getY() + ") " 
       + "Group Counts:"); 
     java.util.Iterator<Entry<Character, Integer>> it = groupCount 
       .entrySet().iterator(); 
     while (it.hasNext()) { 
      Map.Entry<Character, Integer> pairs = (Map.Entry<Character, Integer>) it 
        .next(); 
      System.out.print(pairs.getKey() + " = "); 
      System.out.print(pairs.getValue()); 
      System.out.print(pairs.getValue() > 1 ? " Groups" : "Group"); 
      if (it.hasNext()) { 
       System.out.print(","); 
      } 

      it.remove(); 
     } 
     input.close(); 
    } 

    public static void getLargest() { 
     largestInt = 0; 
     java.util.Iterator<Entry<Character, Integer>> it = map.entrySet() 
       .iterator(); 
     while (it.hasNext()) { 
      Map.Entry<Character, Integer> pairs = (Map.Entry<Character, Integer>) it 
        .next(); 
      if (pairs.getValue() > largestInt) { 
       largestChar = pairs.getKey(); 
       largestInt = pairs.getValue(); 
      } 
      it.remove(); // avoids a ConcurrentModificationException 
     } 
     // System.out.println("Largest Char is " + largestChar); 
     // System.out.println(largestInt); 

    } 

    // method to display the input file. Assuming no grid is no more than 10 
    // rows 
    private static void display(char[][] grid) { 
     for (int i = 0; i < 10; i++) { 
      for (int j = 0; j < 20; j++) { 
       System.out.print(grid[i][j]); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 

    private static void search(char[][] grid) { 
     search(grid, 0, 0); 

    } 

    private static void search(char[][] grid, int x, int y) { 

     for (int i = 0; i < 10; i++) { 
      for (int j = 0; j < 20; j++) { 
       search(grid, i, j, grid[i][j]); 
      } 
     } 

    } 

    private static void search(char[][] grid, int x, int y, char c) { 

     search(grid, x, y, c, groups); 
     if (finished == true) { 
      // System.out.println("GROUP " + groups + " with Character " + c + 
      // " has members of : " + numbers.get(groups)); 
      if (!map.containsKey(c)) { 
       map.put(c, -1); 
       groupCount.put(c, 1); 
      } else { 
       groupCount.put(c, groupCount.get(c) + 1); 
      } 
      if (map.get(c) < numbers.get(groups)) { 
       // System.out.println("OVERWRITE"); 
       map.put(c, numbers.get(groups)); 
      } 
      groups++; 
      finished = false; 
     } 

    } 

    private static void search(char[][] grid, int x, int y, char c, int group) { 
     Point now = new Point(x, y); 

     if (!searched.contains(now)) { 
      // System.out.println(now.toString() + c); 
      finished = true; 
      searched.add(now); 
      while (numbers.size() <= group) { 

       numbers.add(0); 
      } 
      while (groupOrigins.size() <= group) { 
       groupOrigins.add(new Point(-1, -1)); 
      } 
      if (groupOrigins.get(group).equals(new Point(-1, -1))) { 
       groupOrigins.set(group, now); 
      } 

      numbers.set(group, numbers.get(group) + 1); 
      if (y - 1 >= 0) { 
       if (grid[x][y - 1] == c) { 
        search(grid, x, y - 1, c, group); 
       } 
      } 
      if (y + 1 < 20) { 

       if (grid[x][y + 1] == c) { 
        search(grid, x, y + 1, c, group); 
       } 
      } 
      if (x - 1 >= 0) { 
       if (grid[x - 1][y] == c) { 
        search(grid, x - 1, y, c, group); 
       } 
      } 
      if (x + 1 < 10) { 
       if (grid[x + 1][y] == c) { 
        search(grid, x + 1, y, c, group); 
       } 
      } 
     } 
    } 

} 

編輯:編輯以符合要求。

+0

,工作很棒。我很感激你花時間來幫忙。 –

+0

我討厭成爲一個負擔,但是有沒有辦法讓你這樣做,因此輸出結果只顯示最大的組以及該組的起始位置,以及每個角色的組數。 所以它看起來像這樣: 最大的A組是從(0,7)開始的49個成員。 組計數:A = 17組,B = 22組,C = 3組。 如果不是很好,我只是很難修改你的代碼來做到這一點。 @McKevin –

+0

@ user2147096好吧,我已經編輯過符合您的要求,請忘記標記爲答案;)。 –