好的,这是我的任务。我在概念上理解需要做什么,但我在如何执行它时遇到了麻烦。
编写一个程序,接受一个二进制随机字母网格的文件名(作为“命令参数”),并返回A,B组等的名称和组数。(附件是一个例子)正如所写的那样,文本中7.46解决方案的一部分。)
您可以假设网格正好是10行和20列。 最多可以有3个唯一字符(不是上限对算法有影响)。 注意:在处理开始之前,您不需要知道实际存在多少个唯一字符,但假设“上限”只是为您的代码提供了“准备”给定数量的能力。
网格没有“换行”(即从左侧移开不会让你回到右边,就像吃豆人一样) 如果您发现更容易使网格环绕圆环(顶部连接到底部/左侧连接到右侧),请随意这样做。只需表明您已在提交中做出此设计决定。 围绕圆环包裹网格实际上使递归代码变得更小,但它在概念上有点困难。
组是水平或垂直连接的匹配字母的集合。 对角线元素未连接。 您的程序应显示:
每个字母的唯一分组总数。 最大组的字母(和相应的大小)。 例子:
输入文件:
BBBBBABAAAAABAABBBAA
AAAABBABBABBBABAABBA
AAABABABAABBBBBABBAB
BBAAAABAABBBBAABBBAB
BAAABAABAAABBBAAAABA
AABABBAAABBBABBBAABA
BABBAAAABABBBBBAAABB
BABABAABAAAABAABBBAA
BABBAAAABBBABBAAAABB
ABABBBBBABAAABABAAAA
最大的一组是A,49名成员从(0,7)开始 组计数:A = 17组,B = 22组
BBBBBABXXXXXBAABBBAA
XXXXBBABBXBBBABAABBA
XXXBABABXXBBBBBABBAB
BBXXXXBXXBBBBAABBBAB
BXXXBXXBXXXBBBAAAABA
XXBABBXXXBBBABBBAABA
BXBBXXXXBABBBBBAAABB
BXBABXXBAAAABAABBBAA
BXBBXXXXBBBABBAAAABB
ABABBBBBABAAABABAAAA
这就是49的位置(我将A标记为组成49的X)。
这是我到目前为止所做的:
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 :(得分: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);
}
}
}
}
}
编辑:编辑以符合要求。