根据矩阵字母表,寻找矩阵中行方向、列方向、对角线方向的所有单词
详细可以查看数据结构与算法-java语言描述第一章
使用了字典库,字典库可参考文章,算法一样:
程序:
package wordGame;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class WordGame {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
char[][] chars= {
{'l','a','g','e','l'},
{'s','o','n','g','i'},
{'h','a','v','e','s'},
{'u','f','i','e','t'},
{'b','k','r','a','m'}};
List<String> words=findWords(chars);
int count=1;
for(String word:words) {
System.out.print(" "+word);
if((count)%5==0) {
System.out.println("");
}
count++;
}
}
public static List<String> findWords(char[][] chars) throws IOException {
int length=chars.length;
char[][] chars1=new char[length][length];
chars1=chars;
List<String> findword=new ArrayList<String>();
List<String> possibleWord=findPossibleWord(chars1);
findword=trueWord(possibleWord);
return findword;
}
//寻找矩阵中所有长度组合的可能单词
public static List<String> findPossibleWord(char[][] chars){
List<String> possibleWord=new ArrayList<String>();
StringBuffer word=new StringBuffer();
Integer len=chars.length;
for(int i=0;i<len;i++)
for(int j=0;j<len;j++){
if(i==j) {
for(int k=j;k<len;k++){//以第一个字母起一行字母的组合单词/正向
//char temp=chars[i][k];
word=word.append(chars[i][k]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=j;k>=0;k--){//以第一个字母起一行字母的组合单词/反向
word=word.append(chars[i][k]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i;k<chars.length;k++){//以第一个字母起一列字母的组合单词/正向
word=word.append(chars[k][j]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i;k>=0;k--){//以第一个字母起一列字母的组合单词/反向
word=word.append(chars[k][j]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i;k<chars.length;k++){//以第一个字母起对角线字母的组合单词/正向
word=word.append(chars[k][k]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i;k>=0;k--){//以第一个字母起对角线字母的组合单词/反向
word=word.append(chars[k][k]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
}
if((i+j)==(chars.length-1)){
for(int k=i,n=j;k<chars.length;){//以第i个字母起一行字母的组合单词/正向
word=word.append(chars[k++][n--]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i,n=j;k>=0;){//以第i个字母起一行字母的组合单词/反向
word=word.append(chars[k--][n++]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
}
else {
for(int k=j;k<chars.length;k++){//以第i个字母起一行字母的组合单词
word=word.append(chars[i][k]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=j;k>=0;k--){//以第i个字母起一行字母的组合单词
word=word.append(chars[i][k]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i;k<chars.length;k++){//以第i个字母起一列字母的组合单词
word=word.append(chars[k][j]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
for(int k=i;k>=0;k--){//以第i个字母起一列字母的组合单词
word=word.append(chars[k][j]);
String str=new String(word);
possibleWord.add(str);
}
word.delete(0,word.length());
}
}
return possibleWord;
}
//词典单词读入 将词典读入按单词长度分组
public static List<String> trueWord(List<String> possibleWord) throws IOException{
Map<Integer,List<String>> possibleWordMap=new TreeMap<Integer,List<String>>();
possibleWordMap=dividMap(possibleWord);//按单词长短进行分组
//剩下:词典单词读入 将词典读入按单词长度分组Map<Integer,List<String>> 将分组的实例单词同同长度的词典Map进行检查是否是英语单词
Map<Integer,List<String>> dic=readDictionary();
List<String> englishWord=compareAndFind(possibleWordMap,dic);
return englishWord;
}
//同字典进行对比,找出英语单词
public static List<String> compareAndFind(Map<Integer,List<String>>possibleWordMap,Map<Integer,List<String>>dic){
List<String> exitEnglishWord=new ArrayList<String>();
for(Map.Entry<Integer, List<String>> entry:possibleWordMap.entrySet()) {
Integer length=entry.getKey();
List<String> wordlist=entry.getValue();
for(String str:wordlist) {
if(dic.get(length)!=null) {
List<String> dicWord=dic.get(length);
for(String english:dicWord) {
if(str.equalsIgnoreCase(english))//equalsIgnoreCase
{
exitEnglishWord.add(str);
//System.out.println(str);
break;
}
}
}
else System.out.println("dictionary don't hava such length word");
}
}
return exitEnglishWord;
}
//按单词长短进行分组
public static Map<Integer,List<String>> dividMap(List<String> possibleWord){
Map<Integer,Set<String>> wordmap=new TreeMap<Integer,Set<String>>();
for(int i=0;i<possibleWord.size();i++) {
String word =possibleWord.get(i);
int len=word.length();
if(wordmap.get(len)==null) {
Set<String> lset=new TreeSet<String>();
lset.add(word);
wordmap.put(len, lset);
}
else
{
Set<String> set=wordmap.get(len);
set.add(word);
}
}
Map<Integer,List<String>> words=new TreeMap<Integer,List<String>>();
for(Map.Entry<Integer, Set<String>> entry:wordmap.entrySet()) {
Integer length=entry.getKey();
Set<String> set=entry.getValue();
List<String> list=new ArrayList<String>(set);
words.put(length, list);
}
return words;
}
//从txt读入字典并按长度分组
public static Map<Integer,List<String>> readDictionary() throws IOException{
File file=new File("dic\\EnglishUTF-8noBOM.txt");
List<String> list=new ArrayList<String>();
list=read(file);
Map<Integer,List<String>> wordmap=dividMap(list);
return wordmap;
}
//从txt读入字典
public static List<String> read(File file) throws IOException{
List<String> list=new ArrayList<String>();
BufferedReader in=new BufferedReader(new FileReader(file));
String words;
while((words=in.readLine())!=null) {
String[] str=words.trim().split(" ");
list.add(str[0]);
}
in.close();
return list;
}
}
结果:
a i if me no
on so age hub lag
mar ram son have love
mark song