34 lines
1.1 KiB
Text
34 lines
1.1 KiB
Text
|
|
#![feature(core, unicode)]
|
||
|
|
extern crate core;
|
||
|
|
extern crate unicode;
|
||
|
|
|
||
|
|
use core::iter::repeat;
|
||
|
|
use core::str::StrExt;
|
||
|
|
use unicode::char::CharExt;
|
||
|
|
|
||
|
|
fn rec_can_make_word(index: usize, word: &str, blocks: &[&str], used: &mut[bool]) -> bool {
|
||
|
|
let c = word.char_at(index).to_uppercase();
|
||
|
|
for i in range(0, blocks.len()) {
|
||
|
|
if !used[i] && blocks[i].chars().any(|s| s == c) {
|
||
|
|
used[i] = true;
|
||
|
|
if index == 0 || rec_can_make_word(index - 1, word, blocks, used) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
used[i] = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
false
|
||
|
|
}
|
||
|
|
|
||
|
|
fn can_make_word(word: &str, blocks: &[&str]) -> bool {
|
||
|
|
return rec_can_make_word(word.char_len() - 1, word, blocks, repeat(false).take(blocks.len()).collect::<Vec<_>>().as_mut_slice());
|
||
|
|
}
|
||
|
|
|
||
|
|
fn main() {
|
||
|
|
let blocks = [("BO"), ("XK"), ("DQ"), ("CP"), ("NA"), ("GT"), ("RE"), ("TG"), ("QD"), ("FS"), ("JW"), ("HU"), ("VI"), ("AN"), ("OB"), ("ER"), ("FS"), ("LY"), ("PC"), ("ZM")];
|
||
|
|
let words = ["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"];
|
||
|
|
for word in words.iter() {
|
||
|
|
println!("{} -> {}", word, can_make_word(word.as_slice(), blocks.as_slice()))
|
||
|
|
}
|
||
|
|
}
|