zoukankan      html  css  js  c++  java
  • 79. Word Search

    搜词,一个字母只能用一遍。

    比较简单,无非是看4个方向……
    public boolean exist(char[][] board, String word)
    {
    if (board == null) return false;
    int row = board.length;
    int col = board[0].length;

       	boolean[][] visitBoard = new boolean[row][col];
       	for(int m = 0; m < row;m++)
       	{
       		for(int n = 0; n < col;n++)
       		{
       			visitBoard[m][n] = false;
    
       		}
       	}
       
       for(int m = 0; m < row; m++)
       	for(int n = 0; n < col; n++)
       	{
       		if(word.charAt(0) == board[m][n]) 
       		{
       			visitBoard[m][n] = true;
       			if(helper(board,1,word,m,n,visitBoard)) return true;
       			visitBoard[m][n] = false;
       		}
       	}
       	return false;
    }
    
    
    public boolean helper(char[][] board, int at, String word,int m, int n,boolean[][] visitBoard)
    {
    	if(at == word.length()) return true;
    	char tempChar = word.charAt(at);
    	boolean res = false;
    	//up
    	if( n + 1 < board[0].length)
    	{
    		if(tempChar == board[m][n+1] && !visitBoard[m][n+1])
    		{
    			visitBoard[m][n+1] = true;
    			res = res || helper(board,at+1,word,m,n+1,visitBoard);
    			visitBoard[m][n+1] = false;
    		}
    	}
    	//down
    	if( n - 1 >= 0)
    	{
    		if(tempChar == board[m][n-1] && !visitBoard[m][n-1])
    		{
    			visitBoard[m][n-1] = true;
    			res = res || helper(board,at+1,word,m,n-1,visitBoard);
    			visitBoard[m][n-1] = false;
    
    		}
    	}
    	//left
    	if( m-1 >= 0)
    	{
    		if(tempChar == board[m-1][n] && !visitBoard[m-1][n])
    		{
    			visitBoard[m-1][n] = true;
    			res = res || helper(board,at+1,word,m-1,n,visitBoard);
    			visitBoard[m-1][n] = false;
    		}
    	}
    
    	//right
    	if( m+1 < board.length)
    	{
    		if(tempChar == board[m+1][n] &&  !visitBoard[m+1][n])
    		{
    			visitBoard[m+1][n] = true;
    			res = res || helper(board,at+1,word,m+1,n,visitBoard);
    			visitBoard[m+1][n] = false;
    
    		}
    
    	}
    	return res;
    }
    

    写的很乱 改了几次 感觉完全可以写的更好
    再说吧。。




    二刷。

    幸亏二刷昨晚比一刷的描述要好,否则没有进步真让我觉得自己是真蠢。True Dumb..

    BackTrack吧,从每个点开始都试一次,boolean[][]标记使用过的元素,go deeper的条件是 找到 + deeper direction unvisited and in range

    public class Solution 
    {
        public boolean exist(char[][] board, String word) 
        {
            if(board.length == 0) return false;
            boolean[][] check = new boolean[board.length][board[0].length];
            
            for(int i = 0; i < board.length;i++)
            {
                for(int j = 0; j < board[0].length;j++)
                {
                    check[i][j] = true;
                    if(helper(i,j,board,word,check,0)) return true;
                    check[i][j] = false;
                }
            }
            
            return false;
        }
        
        public boolean helper(int x, int y,char[][] board, String word, boolean[][] check,int m)
        {
            if(m == word.length()) return true;
            else
            {
                if(board[x][y] == word.charAt(m))
                {
                    if(m == word.length()-1) return true;
                    if(x > 0 && !check[x-1][y])
                    {
                        check[x-1][y] = true;
                        if(helper(x-1,y,board,word,check,m+1)) return true;
                        check[x-1][y] = false;
                    }
                    if(y > 0 && !check[x][y-1])
                    {
                        check[x][y-1] = true;
                        if(helper(x,y-1,board,word,check,m+1)) return true;
                        check[x][y-1] = false;
                    }
                    if(x < board.length-1 && !check[x+1][y])
                    {
                        check[x+1][y] = true;
                        if(helper(x+1,y,board,word,check,m+1)) return true;
                        check[x+1][y] = false;
                    }
                    if(y < board[0].length-1 && !check[x][y+1])
                    {
                        check[x][y+1] = true;
                        if(helper(x,y+1,board,word,check,m+1)) return true;
                        check[x][y+1] = false;
                    }
                    return false;
                }
                else return false;
            }
            
            
        }
    }
    

    BackTrack的题大多数比较基础,属于pure programming skill,基本和数学无关呀。。




    三刷。
    2D板子上进行DFS的题很多。。
    三刷学着把DFS写简单点,主要是要在DFS的开头进行条件判断。
    然后如果棋盘不是read only可以在棋盘上标记是否访问过,换成别的CHAR之类的再换回来。
    然后如果多一个变量记录比较到WORD中哪一个字母,可以不用反复做substring,substring在java里很不友好。。

    Time: O((mn)4^k) 感觉是这个,每次4个方向,depth是单词长度K,然后每个位置都要来一次。。

    Space: O(mn) 不是read only的话可以O(1)?

    public class Solution {
        public boolean exist(char[][] board, String word) {
            if (board.length == 0 || word.length() == 0) return false;
            boolean[][] visited = new boolean[board.length][board[0].length];
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if(dfs(board, visited, i, j, word)) {
                        return true;
                    }
                    visited[i][j] = false;
                }
            }
            return false;
        }
        
        public boolean dfs(char[][] board, boolean[][] visited, int m, int n, String word) {
            if (word.length() == 0) return true;
            if (m < 0 || m == board.length || n < 0 || n == board[0].length || visited[m][n] || board[m][n] != word.charAt(0)) return false;
            visited[m][n]= true;
            if (dfs(board, visited, m+1, n, word.substring(1)) || dfs(board, visited, m, n+1, word.substring(1)) || 
                dfs(board, visited, m-1, n, word.substring(1)) || dfs(board, visited,m, n-1, word.substring(1))) {
                    return true;
                } else {
                    visited[m][n] = false;
                    return false;
                }   
        }
    }
    
  • 相关阅读:
    第八次作业-非确定的自动机NFA确定化为DFA
    正规式到正规文法与自动机
    正则表达式基本介绍
    博客园源码不可copy的解决办法
    爬取湖北师范大学招生信息网中的专业简介
    利用爬虫爬取LOL官网上皮肤图片
    利用python的requests和BeautifulSoup库爬取小说网站内容
    python图像处理之PIL库
    Python之文件的使用
    python入门之jieba库的使用
  • 原文地址:https://www.cnblogs.com/reboot329/p/5875844.html
Copyright © 2011-2022 走看看