剑指2.4.3回溯法

简单介绍

回溯法可以看作蛮力法的升级版。问题的每一步都有不同的选择,我们在某一步选择了其中一个选项,就进入下一步,然后又面临新的选项。如果到某一项选择
当我们到达某一个节点时,尝试所有可能的选项并在满足条件的前提下递归地抵达下一个节点。

矩阵中的路径

  • 题目描述
    请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
  • 解题思路
    假设某个格子的字符为ch,并且这个格子对应于字符串的第i个字符:
  1. 如果路径上第i个字符恰好时ch,那么我们可以到相邻的格子上寻找第i+1个字符
  2. 如果在相邻格子上都没有找到对应的第i+1个字符,那么我们回到第n-1格子中,重新定位第n个格子
  3. 除了矩阵边界上的格子之外,其他格子都有4个相邻的格子(注意添加判断)
  4. 需要注意的:不能重复使用格子
class Solution {
public:
    bool hasPath(char* matrix, int rows, int cols, char* str)
    {
        if(matrix==nullptr||rows<1||cols<1||str==nullptr)
            return false;
        vector<vector<bool>> visit(rows, vector<bool>(cols, false));//新建访问数组
        int p=0;
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                if(hasPathCore(matrix, rows, cols, i, j, p, visit, str))
                    return true;
            }
        }
        return false;
    }
    bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, int& p, vector<vector<bool>>& visit,const char* str)
    {
        //p指向当前需要寻找的字符index
        if(str[p]=='\0')
            return true;//已经查询完毕
        bool hasPath=false;
        if(0<=p&&row<rows&&0<=row&&col<cols&&0<=col&&matrix[row*cols+col]==str[p]&&!visit[row][col])
        {
            p++;
            visit[row][col]=true;
            hasPath=(hasPathCore(matrix, rows, cols, row+1, col, p, visit, str)||
                     hasPathCore(matrix, rows, cols, row, col+1, p, visit, str)||
                     hasPathCore(matrix, rows, cols, row-1, col, p, visit, str)||
                     hasPathCore(matrix, rows, cols, row, col-1, p, visit, str));
            if(!hasPath)//无法找到符合的p+1字符
            {
                visit[row][col]=false;//当前节点设为未访问
                p--;//回溯到上一个定位
            }
        }
        return hasPath;
    }


};

机器人的运动范围

题目描述

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

解题思路

三个函数:

  • 主函数
  • 递归函数
  • 获取digitnum
  • 判断是否可行
class Solution {
public:
    int movingCount(int threshold, int rows, int cols)
    {
        vector<vector<bool>> visited(rows, vector<bool>(cols, false));
        int row=0, col=0;
        return movCore(row, col, cols, rows, threshold, visited);
    }
    int movCore(int row, int col, int cols, int rows, int threshold, vector<vector<bool>>& visited)
    {
        int count=0;
        if(check(row, col, rows, cols, threshold)&&!visited[row][col])
        {
            count++;
            visited[row][col]=true;
            count+=(movCore(row+1, col, cols, rows, threshold, visited)+
                    movCore(row, col+1, cols, rows, threshold, visited)+
                    movCore(row-1, col, cols, rows, threshold, visited)+
                    movCore(row, col-1, cols, rows, threshold, visited)
                   );
        }
        return count;
    }
    int getDigit(int num)
    {
        int count=0;//当前数字的数位之和
        while(num>0)
        {
            count+=num%10;
            num = num/10;
        }
        return count;
    }
    bool check(int row, int col, int rows, int cols, int k)
    {
        if(0<=row&&row<rows&&0<=col&&col<cols)
        {
            int digitnum=getDigit(row)+getDigit(col);//分别计算行和列的数位之和并求和
            if(digitnum<=k)//如果没有超过阈值则正确
                return true;
        }
        return false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值