首页 > 搜索 > DFS搜索 > POJ 1501 Word-Search Wonder [解题报告] Java
2013
11-09

POJ 1501 Word-Search Wonder [解题报告] Java

Word-Search Wonder

问题描述 :

The Pyrates Restaurant was starting to fill up as Valentine McKee walked in. She scanned the crowd for her sister, brother-in-law, and nephew. Seeing her sister waving from the far end of the restaurant, she made her way back to their booth. “Hi, Valentine,” her sister and brother-in-law, Niki and Dennis Chapman, greeted her.

“Hi, guys,” she replied. “What are you doing, Wade?” she asked her nephew. He was busy working on one of the restaurant’s activity sheets with a crayon.

“I’m doing a word search game,” Wade explained. “I have to find all of these words in this big mess of letters. This is really hard.” Wade looked intently at the paper in front of him.

“Can I help?” asked Valentine, looking across the table at the activity sheet.

“Sure. These are the words we’re looking for. They’re the names of different kinds of Planes, Trains, and Automobiles.”

输入:

The first line of input will specify the length (in characters) of the sides of the letter matrix (the matrix of letters will be square). The length, l, will be in the range 1 <= l <= 100. The next l lines of input will be the matrix itself, each line will contain l uppercase letters.

A list of words will follow. Each word will be on a line by itself; there will be 100 or fewer words. Each word will be 100 or fewer characters long, and will only contain uppercase letters.

The final line of input will contain a single zero character.

输出:

Your program should attempt to find each word from the word list in the puzzle. A word is “found” if all the characters in the word can be traced in a single (unidirectional) horizontal, vertical, or diagonal line in the letter matrix. Words may not “wrap around” rows or columns, but horizontal and diagonal words may proceed from right to left (“backwards”). For each word that is found, your program should print the coordinates of its first and last letters in the matrix on a single line, separated by a single space. Coordinates are pairs of comma-separated integers (indexed from 1), where the first integer specifies the row number and the second integer specifies the column number.

If a word is not found, the string “Not found” should be output instead of a pair of coordinates.

Each word from the input can be “found” at most once in the puzzle.

样例输入:

5
EDEEE
DISKE
ESEEE
ECEEE
EEEEE
DISC
DISK
DISP
0

样例输出:

1,2 4,2
2,1 2,4
Not found

解题代码:

import java.util.Arrays;
import java.util.Scanner;
public class Main{
 private char c[][];  
 private int n;  
 private String s;
 private boolean flag;
 private boolean vis[][];  
 private int sx,sy,ex,ey;//匹配成功后,输出的首字母和末字母匹配成功的位置

 private int dir[][]={{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1}};  

   public Main(int n,String s,char[][] c){
      this.n=n;
      this.s=s;
      this.c=c;
      flag=false;
      vis=new boolean[n][n];
   }


  private void dfs(int i,int j,int m,int k) {  
    int x,y;  
    if(flag) return;  
     //深搜结束条件
   if((c[i][j]==s.charAt(s.length()-1))&&m==s.length())//必须加个m==s.length()因为给出的字符串可能有重复字母   
   {  
      ex=i;ey=j;  //记录匹配成功末字符位置
      flag=true;   
      return;  
     }  
    x=i+dir[k][0];  
    y=j+dir[k][1];  
    if(x>=0&&x< n&&y>=0&&y< n&&c[x][y]==s.charAt(m)&&!vis[x][y])  
    {  
      vis[x][y]=true;  
      dfs(x,y,m+1,k);  //同一方向,搜索下一个字符
      vis[x][y]=false;  
    }  
  }  
  
   public static void main(String args[]){ 
     Scanner in=new Scanner(System.in);
     String s="";
     int n=in.nextInt();  
     char[][] c=new char[n][n];
 
     for(int i=0;i< n;i++){
      s=in.next();
       for(int j=0;j< n;j++)
          c[i][j]=s.charAt(j);
      }

     
     while(true)  { 
      s=in.next();
      if(s.equals("0")) break;  
       Main main=new Main(n,s,c);
       main.go();
     }
   }

 private void go(){
  
     for(int i=0;i< n;i++)
       for(int j=0;j< n;j++)  
         if(c[i][j]==s.charAt(0)){//找到第一个字母后,按同一个方向搜索   
           for(int k=0;k< 8;k++){  
             dfs(i,j,1,k);  
             if(flag)   
             {   
               sx=i;sy=j;   
               System.out.printf("%d,%d %d,%d\n",sx+1,sy+1,ex+1,ey+1); 
               return;
             }  
           }  
         }  
      
       if(!flag) System.out.printf("Not found\n");  
   }  
  
}

  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。