zoukankan      html  css  js  c++  java
  • ## B--1-数据结构(5)--图

    B--1-数据结构(5)--图

    题1 矩阵中的路径

    和"二叉树中和为某一值的路径"一题比较

    说白了就是换了个数据结构,换一下讨论情况

    1. 返回值变成bool
    2. 左子树和右子树 变成了 矩阵中的四个方位
    3. 要避免重复访问节点(二叉树就不用)
    4. 出发节点从二叉树的顶点,变成了从矩阵中的任何一个点,每个点都要算
    5. base case 由 到达叶子节点且sum==target 变成了 index == size()
    class Solution {
    public:
        bool exist(vector<vector<char>>& board, string word) {
            if ( board.empty() || board[0].empty())
            {
                return word.empty();
            }
            for( int i = 0 ; i < board.size() ; i ++ ){
                for ( int j = 0 ; j < board[0].size() ; j ++ ){
                    if (process( board , word , i , j , 0))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    
       struct position{
            position (int a , int b) 
            {
                x = a;
                y = b;
            }
            int x;
            int y;
        };
    
            // 从[ row , col ] 开始 dfs ,判断路径中是否有 == word 的
        bool process(vector<vector<char>>& board , string word , int row , int col , int index )
        {
            position pos  = position(row , col);
    
            // base case : 走到最后了都没被检查出错误返回
            if ( index == word.size() )
            {
                return true;
            }
            if ( pos.x < 0 || pos.x >= board.size() || pos.y < 0 || pos.y  >= board[0].size() )
            {
                return false;
            }
    
            // 根据当前格子的状态-->给值
            if ( word[index] != board[pos.x][pos.y])
            {
                return false;
            }
            else if (word[index] != board[pos.x][pos.y] )
            {
            board[pos.x][pos.y] = '*';      // 用*表示走过的格子
            if (   process ( board , word, row+1 , col , index + 1) ||
                    process ( board , word, row-1 , col ,index + 1) ||
                    process ( board , word, row , col+1,index + 1) ||
                    process ( board , word, row , col-1,index + 1) )
                {
                        return true;
                }
            }
    
            // 回溯 :   发现走错了,这步往下走怎么走都不对,就把值改回来
            board[pos.x][pos.y] = word[index];
            return false;
        }
    };
    
    题2 机器人的运动范围
    class Solution {
    public:
    
        struct position{
            int x ;
            int y ; 
            position(int a ,int b){  x = a ; y = b ;  }
        };
    
        int movingCount(int m, int n, int k) {
            vector<vector<bool>> visit (m , vector<bool>(n,false));
            int res = 0;
            process(0, 0, m , n , k , visit , res);
            return res;
        }
    
        bool accessible( position pos , int k )
        {
            return ( summ( pos.x ) + summ( pos.y ) <= k ) ?  true : false ;
        }
    
        int summ ( int n )
        {
            int num = 0 ;
            while ( n  !=  0 )
            {
                num = num + (n % 10) ; 
                n = n/10; 
            }
            return num;
        }
    
      void process ( int i , int j , int m , int n , const int k , vector<vector<bool>>& visit , int& res)
        {
            position pos = position( i , j ); 
    
            if ( pos.x < 0 || pos.x >= m || pos.y  < 0 || pos.y >= n )
            {
                return ;
            }
            if ( accessible( pos, k ) == false  )
            {
                return;
            }
            if ( visit[pos.x][pos.y] == true )
            {
                return ;
            }
            
            visit[pos.x][pos.y] = true;
            res ++;
    
            process(  i+1 , j , m, n , k ,visit ,res);
            process(  i-1 , j ,m , n , k , visit ,res);
            process(  i, j+1 ,m , n , k , visit ,res);
            process(  i,   j-1,m , n , k , visit ,res);
        }
    };
    
    干啥啥不行,吃饭第一名
  • 相关阅读:
    Scrum Meeting 6 -2014.11.12
    Scrum Meeting 5 -2014.11.11
    Bing词典vs有道词典比对测试报告——体验篇之成长性及用户控制权
    团队项目的用户需求及反馈
    Scrum Meeting 4 -2014.11.8
    Scrum Meeting 3 -2014.11.5
    bing词典vs有道词典对比测试报告——功能篇之细节与用户体验
    Bing词典vs有道词典比对测试报告——功能篇之辅助功能,差异化功能及软件的效能
    Bing词典vs有道词典比对测试报告
    hdu 5087 次长升序串的长度
  • 原文地址:https://www.cnblogs.com/jiangxinyu1/p/12407658.html
Copyright © 2011-2022 走看看