public class BF_String {
public static void main(String[] args){
char[][] ccol={{'D','H','O','B','S','H','N','E','P','T','U','N','E','Y','T','M'},
{'U','E','J','I','H','U','N','Y','S','T','H','A','O','R','T','M'},
{'D','N','A','U','U','E','E','E','M','A','E','N','W','A','T','M'},
{'W','N','A','I','P','L','U','T','O','N','A','O','D','H','T','M'},
{'A','G','H','P','L','I','Z','O','O','E','R','U','S','U','T','M'},
{'R','D','E','I','H','C','T','M','N','W','T','N','S','H','T','M'},
{'F','H','Y','H','O','P','B','E','O','Q','H','I','U','E','T','M'},
{'R','A','C','O','E','A','A','R','R','T','E','O','A','E','T','M'},
{'U','S','A','T','U','R','N','C','P','L','A','N','E','T','T','M'},
{'R','T','A','E','H','F','T','U','E','U','L','E','E','E','T','M'},
{'I','E','U','C','U','F','A','R','O','V','C','E','I','O','T','M'},
{'A','R','F','A','I','R','A','Y','A','O','E','I','R','H','T','M'},
{'T','O','A','I','N','I','A','B','E','A','R','N','A','E','T','M'},
{'O','I','A','T','E','O','E','N','A','A','E','H','U','A','T','M'},
{'E','D','I','D','D','O','E','D','U','T','S','E','T','S','T','M'},
{'E','S','Z','E','E','H','O','P','H','S','L','U','M','S','T','M'}};//字符列表
// System.out.println(ccol.length+","+ccol[0].length);
String[] word={"VENUS","EARTH","MARS","CERES","ASTEROIDS","JUPITER","SATURN","NEPTUNE","URANUS","PLUTO","DWARF","PLANET","MOON"};//词集合
preprocess(ccol,word);
}
//横向、纵向、斜向遍历字符列表
public static void preprocess(char[][] ccol,String[] word){
String[] location=new String[word.length];//记录词在字符列表中的位置
for(int i=0;i<location.length;i++)
location[i]="";
//横向搜索
for(int i=0;i<ccol.length;i++){
bf(String.valueOf(ccol[i]),word,location,i,0,1);
}
//纵向搜索
for(int j=0;j<ccol[0].length;j++){
String ss="";
for(int t=0;t<ccol.length;t++)
ss+=ccol[t][j];
bf(ss,word,location,0,j,2);
}
//上三角斜向搜索
for(int j=0;j<ccol[0].length-1;j++){
String ss="";
String ss1="";
for(int i=0,t=j;i<ccol.length&&t<ccol[0].length;i++,t++){
ss+=ccol[i][t];
ss1+=ccol[i][ccol[0].length-1-t];
}
bf(ss,word,location,0,j,3);
bf(ss1,word,location,0,ccol[0].length-1-j,4);
}
//下三角斜向搜索
for(int i=1;i<ccol.length-1;i++){
String ss="";
String ss1="";
for(int j=0,t=i;j<ccol[0].length-1&&t<ccol.length;j++,t++){
ss+=ccol[t][j];
ss1+=ccol[t][ccol[0].length-1-j];
// System.out.println(ss1);
}
bf(ss,word,location,i,0,5);
bf(ss1,word,location,i,ccol[0].length-1,6);
}
print(ccol,word,location);
}
//判断该字符串是否包含集合中的某个词。参数str代表搜索所得的字符列表组成的串,startx,starty代表该串在ccol中的行和列,flag代表该串str在ccol中的搜索方式如横向搜索
public static void bf(String str,String[] word,String[] location,int startx,int starty,int flag){
for(int i=0;i<word.length;i++){
int first=str.indexOf(word[i]);
if(first!=-1){
if(flag==1){
location[i]+="("+flag+","+startx+","+(starty+first)+")"; //横向搜索 记录该词在ccol中的起始位置以及搜索方向
}
if(flag==2){
location[i]+="("+flag+","+(startx+first)+","+starty+")"; //纵向搜索 记录该词在ccol中的起始位置以及搜索方向
}
if(flag==3){
location[i]+="("+flag+","+(startx+first)+","+(starty+first)+")"; //上三角正向搜索 记录该词在ccol中的起始位置以及搜索方向
}
if(flag==4){
location[i]+="("+flag+","+(startx+first)+","+(starty-first)+")"; //上三角反向搜索 记录该词在ccol中的起始位置以及搜索方向
}
if(flag==5){
location[i]+="("+flag+","+(startx+first)+","+(starty+first)+")"; //下三角正向搜索 记录该词在ccol中的起始位置以及搜索方向
}
if(flag==6){
location[i]+="("+flag+","+(startx+first)+","+(starty-first)+")"; //下三角反向搜索 记录该词在ccol中的起始位置以及搜索方向
}
}
}
}
//打印所有词在字符列表中出现的位置
public static void print(char[][] ch, String[] word,String[] loc){
for(int i=0;i<word.length;i++){
System.out.println(word[i]+": "+loc[i]);
}
}
}