September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,44 +1,36 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ABC{
|
||||
private static void swap(int i, int j, Object... arr){
|
||||
Object tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
|
||||
public static boolean canMakeWord(String word, String... blocks) {
|
||||
if(word.length() == 0)
|
||||
return true;
|
||||
|
||||
char c = Character.toUpperCase(word.charAt(0));
|
||||
for(int i = 0; i < blocks.length; i++) {
|
||||
String b = blocks[i];
|
||||
if(Character.toUpperCase(b.charAt(0)) != c && Character.toUpperCase(b.charAt(1)) != c)
|
||||
continue;
|
||||
swap(0, i, blocks);
|
||||
if(canMakeWord(word.substring(1), Arrays.copyOfRange(blocks, 1, blocks.length)))
|
||||
return true;
|
||||
swap(0, i, blocks);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
String[] blocks = {"BO", "XK", "DQ", "CP", "NA",
|
||||
"GT", "RE", "TG", "QD", "FS",
|
||||
"JW", "HU", "VI", "AN", "OB",
|
||||
"ER", "FS", "LY", "PC", "ZM"};
|
||||
public class ABC {
|
||||
|
||||
System.out.println("\"\": " + canMakeWord("", blocks));
|
||||
System.out.println("A: " + canMakeWord("A", blocks));
|
||||
System.out.println("BARK: " + canMakeWord("BARK", blocks));
|
||||
System.out.println("book: " + canMakeWord("book", blocks));
|
||||
System.out.println("treat: " + canMakeWord("treat", blocks));
|
||||
System.out.println("COMMON: " + canMakeWord("COMMON", blocks));
|
||||
System.out.println("SQuAd: " + canMakeWord("SQuAd", blocks));
|
||||
System.out.println("CONFUSE: " + canMakeWord("CONFUSE", blocks));
|
||||
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
List<String> blocks = Arrays.asList(
|
||||
"BO", "XK", "DQ", "CP", "NA",
|
||||
"GT", "RE", "TG", "QD", "FS",
|
||||
"JW", "HU", "VI", "AN", "OB",
|
||||
"ER", "FS", "LY", "PC", "ZM");
|
||||
|
||||
for (String word : Arrays.asList("", "A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE")) {
|
||||
System.out.printf("%s: %s%n", word.isEmpty() ? "\"\"" : word, canMakeWord(word, blocks));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canMakeWord(String word, List<String> blocks) {
|
||||
if (word.isEmpty())
|
||||
return true;
|
||||
|
||||
char c = word.charAt(0);
|
||||
for (int i = 0; i < blocks.size(); i++) {
|
||||
String b = blocks.get(i);
|
||||
if (b.charAt(0) != c && b.charAt(1) != c)
|
||||
continue;
|
||||
Collections.swap(blocks, 0, i);
|
||||
if (canMakeWord(word.substring(1), blocks.subList(1, blocks.size())))
|
||||
return true;
|
||||
Collections.swap(blocks, 0, i);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue