zoukankan      html  css  js  c++  java
  • 剑指offer 机器人的运动范围

    题目:

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

    代码:

     1 class Solution {
     2 public:
     3     int movingCount(int threshold, int rows, int cols)
     4     {
     5         bool* finish = new bool[rows*cols]();
     6         return Count(threshold, rows, cols, finish, 0, 0 );
     7     }
     8     int Count(int threshold, int rows, int cols, bool* fin, int x, int y ){
     9         if(x < 0 || x >=rows || y < 0 || y >= cols || fin[x*cols+y] ||BitSum(x)+BitSum(y) > threshold )
    10             return false;
    11         fin[x*cols+y] = true;
    12         return Count(threshold, rows, cols, fin, x - 1, y )
    13             + Count(threshold, rows, cols, fin, x + 1, y )
    14             + Count(threshold, rows, cols, fin, x, y - 1 )
    15             + Count(threshold, rows, cols, fin, x, y + 1 )
    16             + 1;
    17     }
    18     int BitSum(int t) {
    19         int count = 0;
    20         while(t){
    21             count += t %10;
    22             t /= 10;
    23         }
    24         return count;
    25     }
    26 };

    我的笔记: 

      从(0,0)开始走,每成功走一步标记当前位置为true,然后从当前位置往四个方向探索,
    返回1 + 4 个方向的探索值之和。
      探索时,判断当前节点是否可达的标准为:
    • 1)当前节点在矩阵内;
    • 2)当前节点未被访问过;
    • 3)当前节点数值满足限制。
  • 相关阅读:
    MuJS官网示例讲解
    Windows下用Bochs编译运行Linux-0.11(转)
    mysql数据库设置远程连接权限
    Courses in Computer Science and Engineering
    docker-ce 安装和卸载
    实现自定义docker 镜像共享
    cmakelists.txt中配置openg环境出现: undefined reference to symbol 'glLightfv'
    ubuntu16.04如何查看内存和CPU的使用情况
    ROS
    QT_OPENGL-------- 5.model
  • 原文地址:https://www.cnblogs.com/john1015/p/13140931.html
Copyright © 2011-2022 走看看