1. 程式人生 > >找詞”遊戲是在美國流行的一種遊戲,它要求遊戲者從一張填滿字母的正方形表中,找出包含在一個給定集合中的所有詞。這些詞可以豎著讀(向上或向下)、橫著讀(從左或從右),或者沿45度對角線斜著讀。

找詞”遊戲是在美國流行的一種遊戲,它要求遊戲者從一張填滿字母的正方形表中,找出包含在一個給定集合中的所有詞。這些詞可以豎著讀(向上或向下)、橫著讀(從左或從右),或者沿45度對角線斜著讀。


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]);
      }
  }
}