阿晨1998
我做了大多数人可能认为是蛮力方法的事情。本质上,我通过为每个块维护一个独立的索引来按顺序创建每个可能的单词。然后我将创建的词与所需的词进行比较。我还打印出用于构建每个成功的位置。一些额外的开销是我允许具有不同面数的块。最后,可能存在错误。 import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Blocks { public static void main(String[] args) { // input parameters int nBlocks = 5; String wordToFind = "nifty"; // block sizes may have different number of sides // This permits optimization by removing irrelevant letters // (which I didn't do). String[] blockStrings = { "ffi", "rnnf", "aeif", "drst","vyxksksksksky" }; // Split the blocks into a list of letter arrays List<String[]> blocks = Arrays.stream(blockStrings).map(s -> s.split("")).collect( Collectors.toList()); // turn "foobar" into abfoor" String[] word = wordToFind.split(""); String sortedWord = Arrays.stream(word).sorted().collect(Collectors.joining()); int count = 0; int[] k = new int[nBlocks]; String[] w = new String[nBlocks]; // calculate maximum number of iterations. The product // of all the block's faces for n blocks. int end = blocks.stream().mapToInt(a -> a.length).reduce(1, (a, b) -> a * b); for (int ii = 0; ii < end; ii++) { List<Integer> usedBlockPositions = new ArrayList<>(); for (int i = 0; i < nBlocks; i++) { w[i] = blocks.get(i)[k[i]]; usedBlockPositions.add(k[i]); } // compare sorted word to sorted "found" word to see if there is // a match. if (sortedWord.equals( Arrays.stream(w).sorted().collect(Collectors.joining()))) { count++; System.out.println(Arrays.toString(w) + " " + usedBlockPositions); } // Bump the indices to the blocks for next try. This is used to // index into each block face to get the next letter. Once // again, this is written to allow variable faced blocks. // k starts out as [0,0,0,0] // then goes to [1,0,0,0], [2,0,0,0] etc thru to [n1,n2,n3,n4] where // n* is the max number of block faces for given block. The size of // k is the number of blocks (this shows 4). for (int i = 0; i < k.length; i++) { int v = k[i]; if (v >= blocks.get(i).length - 1) { k[i] = 0; } else { k[i]++; break; } } } String format = count != 1 ? "%nThere were %d combinations found.%n" : "%nThere was %d combination found.%n"; System.out.printf(format, count); } }发布的代码打印以下内容。[f, n, i, t, y] [0, 1, 2, 3, 1][f, n, i, t, y] [1, 1, 2, 3, 1][f, n, i, t, y] [0, 2, 2, 3, 1][f, n, i, t, y] [1, 2, 2, 3, 1][i, n, f, t, y] [2, 1, 3, 3, 1][i, n, f, t, y] [2, 2, 3, 3, 1][f, n, i, t, y] [0, 1, 2, 3, 12][f, n, i, t, y] [1, 1, 2, 3, 12][f, n, i, t, y] [0, 2, 2, 3, 12][f, n, i, t, y] [1, 2, 2, 3, 12][i, n, f, t, y] [2, 1, 3, 3, 12][i, n, f, t, y] [2, 2, 3, 3, 12]There were 12 combinations found.