先写上我的代码:

我总是不知道何时把任务交给下一个递归。以致于,写出的代码很臃肿!

 放上别人递归的简洁代码:

bool exist(char** board, int m, int n, char* word) {
    if(word == NULL)    return true;
    if(board == NULL || m*n == 0)   return false;
    
    bool ans= false;
    
    bool **used = (bool**)malloc(m*sizeof(bool*));
    
    for(int i = 0; i < m; i++)
    {
        used[i] = (bool*)malloc(n*sizeof(bool));
        memset(used[i],false,n*sizeof(bool));
    }
    
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(exist_from(board,used,i,j,m,n,word,0))   
            {
                ans = true;
                goto exit;
            }
        }
    }
exit: 
    for(int i = 0; i < m; i++)
    {
        free(used[i]);
    }
    free(used);
    return ans;
}
bool exist_from(char **board,bool ** used, int row, int col, int m, int n, char *word, int k)    //find kth
{
    
    if(word[k] == 0)    return true;    //the end of the string
    
    if(row >=m ||row <0 || col >=n || col < 0)  return false;    //out of range
    
    if(used[row][col] || word[k] != board[row][col])  return false;  //dumplicates or not equal
    
    used[row][col] = true;

    if(exist_from(board,used,row-1,col,m,n,word,k+1) || exist_from(board,used,row+1,col,m,n,word,k+1)||
       exist_from(board,used,row,col-1,m,n,word,k+1)||exist_from(board,used,row,col+1,m,n,word,k+1))
        return true;
    
    used[row][col] = false;
    
    return false;
}

 

非常不递归风格的代码。。

bool exist_from(char **board,bool ** used, int row, int col, int m, int n, char *word, int k);   //find kth


bool exist(char** board, int m, int n, char* word) {
    if(word == NULL)    return true;
    if(board == NULL || m*n == 0)   return false;
    
    bool **used = (bool**)malloc(m*sizeof(bool*));
    
    for(int i = 0; i < m; i++)
    {
        used[i] = (bool*)malloc(n*sizeof(bool));
        memset(used[i],false,n*sizeof(bool));
    }
    
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(word[0] == board[i][j])
            {
                used[i][j] = true;
                if(exist_from(board,used,i,j,m,n,word,1))   return true;
                used[i][j] = false;
            }
        }
    }
    
    return false;
}

bool exist_from(char **board,bool ** used, int row, int col, int m, int n, char *word, int k)    //find kth
{
    bool ans= false;
    
    if(word[k] == 0)    return true;
    
    if(row > 0 && !used[row-1][col] && board[row-1][col] == word[k] )
    {
        used[row-1][col] = true;
        ans = exist_from(board,used,row-1,col,m,n,word,k+1);
        used[row-1][col] = false;
        
        if(ans == true) return true;
    }
    if(row < m-1 && !used[row+1][col] && board[row+1][col] == word[k] )
    {
        used[row+1][col] = true;
        ans = exist_from(board,used,row+1,col,m,n,word,k+1);
        used[row+1][col] = false;
        
        if(ans == true) return true;
    }
    
    if(col > 0 && !used[row][col-1] && board[row][col-1] == word[k] )
    {
        used[row][col-1] = true;
        ans = exist_from(board,used,row,col-1,m,n,word,k+1);
        used[row][col-1] = false;
        
        if(ans == true) return true;
    }
    if(col < n-1 && !used[row][col+1] && board[row][col+1] == word[k] )
    {
        used[row][col+1] = true;
        ans = exist_from(board,used,row,col+1,m,n,word,k+1);
        used[row][col+1] = false;
        
        if(ans == true) return true;
    }
    
    return false;
}

 

其实,如果把范围判断放在更深层,会写出更简洁的代码。。

posted on 2017-12-14 12:21  newbird2017  阅读(126)  评论(0编辑  收藏  举报