剑指offer:矩阵中的路径、机器人的运动范围

剑指offer:矩阵中的路径、机器人的运动范围

题目:

矩阵中的路径:

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符路径。路径可以从矩阵的任一格开始,每一步可以在矩阵中向左右上下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。

思路:

深度优先搜索。

实现:

class Solution {
public:
    bool hasPath(char* matrix, int rows, int cols, char* str)
    {
      /*rows是矩阵的行数,下标为0~rows - 1;cols同理 */
      if(matrix == nullptr || str == nullptr) return false;
      int visited[rows * cols];  //visited记录当前节点是否被访问到。
      memset(visited, 0, sizeof(visited));
      for (int i = 0; i < rows; i++)
      {
        for (int j = 0; j < cols; j++)
        {
          if(dfs(matrix, visited, rows, cols, i, j, 0, str)) return true;
        }
      }
      return false;
    }
    bool dfs(char* matrix, int* visited, int rows, int cols, int i, int j, int k, char* str)
    /*k是标示当前到str中的第几个字符 */
    {
      if (i < 0|| i >= rows || j < 0 || j >= cols || matrix[i * cols + j] != str[k] || visited[i * cols + j]) return false;
      if (k == strlen(str) - 1) return true;  //递归出口
      visited[i * cols + j] = 1; //递
      if (dfs(matrix, visited, rows, cols, i-1, j, k+1, str) ||
          dfs(matrix, visited, rows, cols, i+1, j, k+1, str) ||
          dfs(matrix, visited, rows, cols, i, j-1, k+1, str) ||
          dfs(matrix, visited, rows, cols, i, j+1, k+1, str))
        return true;
      visited[i * cols + j]= 0; //归
      return false;
    }
};

注:在dfs函数中,第一步就要排除各种递归出口。

常见的递归出口有:

  • 越界
  • 不满足题目条件
  • 已经访问过
  • 已经得到答案

后面如果在当前节点没能完成搜索,记得最后要重新标记该节点为未访问。

题目:机器人的运动范围

地上有一个m行n列的方格。一个机器人从坐标(0, 0)的格子开始移动,它每次可以向左、右、上、下移动一格,

但是不能进入行坐标和列坐标的数位之和大于k的格子。例如:当k = 18时,机器人能够进入方格(35, 37),因为3+5+3+7 = 18。但不能进入方格(35,38),因为3+5+3+8 = 19。求该机器人能够到达多少个格子。

思路:深度优先搜索

这个题目可以作为第一个题目的练习题,完全类似。

class Solution {
public:
    int movingCount(int threshold, int rows, int cols)
    {
        /*rows、cols表示行数和列数*/
        int visited[rows * cols];
        memset(visited, 0, sizeof(visited)); //visited 表示当前位置是否被搜索到
        int count = 0;                       //count 记录当前的符合要求的点
        dfs(threshold, rows, cols, 0, 0, &count, visited);  //使用深度优先搜索
        return count;
    }
private:
    void dfs(int threshold, int rows, int cols, int i, int j, int* count, int* visited)
    { 
        if (i < 0 || i >= rows || j < 0 || j >= cols || visited[i * cols + j] == 1 || Satisfy(threshold, i, j) == false) return;
        visited[i * cols + j] = 1; 
        *count += 1;  //找到了一个合条件的
        //向四周找
        dfs(threshold, rows, cols, i-1, j, count, visited);
        dfs(threshold, rows, cols, i+1, j, count, visited);
        dfs(threshold, rows, cols, i, j-1, count, visited);
        dfs(threshold, rows, cols, i, j+1, count, visited);
        return;
    }
    bool Satisfy(int threshold, int i, int j)
    {
        int sum = 0;
        while(i != 0)
        {
            sum += i % 10;
            i /= 10;
        }
        while(j != 0)
        {
            sum += j % 10;
            j /= 10;
        }
        if (sum <= threshold) return true;
        else return false;
    }
};

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值