zoukankan      html  css  js  c++  java
  • (笔试题)机器人的运动范围

    题目:

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

    思路:

    题目求的是机器人能够达到多少个格子,并不是能走多少格子,因此不是一个路径的问题,如题目说的求的是运动范围。

    1、递归

    机器人每到一个位置,判断该位置是否有效,如果有效(是否在方格里,是否已经访问过,是否大于k),往左右上下移动,重复上述行为,最终遍历的范围就是运动的范围。

    2、非递归

    将递归转为非递归,可以用栈或者队列实现,这里并没有遍历的先后顺序,因此队列也是OK的。

    代码:

    1、递归实现

    class Solution {
    public:
        void getMovingCount(int r,int c,int rows,int cols,int &count,vector<bool> &visited,int threshold){
            if(r<0 || r>=rows || c<0 || c>=cols || visited[r*cols+c]==true || getNum(r,c)>threshold)
                return;
            visited[r*cols+c]=true;
            count++;
            getMovingCount(r-1,c,rows,cols,count,visited,threshold);
            getMovingCount(r+1,c,rows,cols,count,visited,threshold);
            getMovingCount(r,c-1,rows,cols,count,visited,threshold);
            getMovingCount(r,c+1,rows,cols,count,visited,threshold);
        }
        int movingCount(int threshold, int rows, int cols){
            vector<bool> visited(rows*cols);
            int count=0;
            getMovingCount(0,0,rows,cols,count,visited,threshold);
            return count;
        }
        int getNum(int r,int c){
            int sum=0;
            while(r){
                sum+=r%10;
                r=r/10;
            }
            while(c){
                sum+=c%10;
                c=c/10;
            }
            return sum;
        }
    };

    2、队列、栈的非递归实现

    struct node {
        int x, y;
        node(int xx,int yy):x(xx),y(yy){}
    };
    class Solution {
    private:
        int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    public:
        int movingCount(int threshold, int rows, int cols) {
            if(threshold<0)
                return 0;
            vector<bool> visited(rows*cols,false);
            int sum = 1;
            node s(0,0);
    //        s.x = s.y = 0;
            queue<node> q;
            q.push(s);
            visited[0] = true;
            while(!q.empty()) {
                node tmp = q.front();
                q.pop();
      
                for(int i = 0; i < 4; i ++) {
                    s.x = tmp.x + d[i][0];
                    s.y = tmp.y + d[i][1];
                    int index = s.x*cols + s.y;
                    if(s.x < 0 || s.x >= rows || s.y < 0 || s.y >= cols || visited[index]==true) continue;
                     
                    if(getNum(s.x,s.y) <= threshold) {
                        sum++;
                        visited[index] = true;
                        q.push(s);
                    }
                }
            }
            return sum;
        }
        int getNum(int r,int c){
            int sum=0;
            while(r){
                sum+=r%10;
                r=r/10;
            }
            while(c){
                sum+=c%10;
                c=c/10;
            }
            return sum;
        }
    };

    struct node {
        int x, y;
        node(int xx,int yy):x(xx),y(yy){}
    };
    class Solution {
    private:
        int d[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    public:
        int movingCount(int threshold, int rows, int cols) {
            if(threshold<0)
                return 0;
            vector<bool> visited(rows*cols,false);
            int sum = 1;
            node s(0,0);
    //        s.x = s.y = 0;
            stack<node> q;
            q.push(s);
            visited[0] = true;
            while(!q.empty()) {
                node tmp = q.top();
                q.pop();
      
                for(int i = 0; i < 4; i ++) {
                    s.x = tmp.x + d[i][0];
                    s.y = tmp.y + d[i][1];
                    int index = s.x*cols + s.y;
                    if(s.x < 0 || s.x >= rows || s.y < 0 || s.y >= cols || visited[index]==true) continue;
                     
                    if(getNum(s.x,s.y) <= threshold) {
                        sum++;
                        visited[index] = true;
                        q.push(s);
                    }
                }
            }
            return sum;
        }
        int getNum(int r,int c){
            int sum=0;
            while(r){
                sum+=r%10;
                r=r/10;
            }
            while(c){
                sum+=c%10;
                c=c/10;
            }
            return sum;
        }
    };
  • 相关阅读:
    WDNMD--冲刺日志(第二天)
    spring boot: filter/interceptor/aop在获取request/method参数上的区别(spring boot 2.3.1)
    spring boot:使用validator做接口的参数、表单、类中多字段的参数验证(spring boot 2.3.1)
    spring boot:使接口返回统一的RESTful格式数据(spring boot 2.3.1)
    spring boot:使用log4j2做异步日志打印(spring boot 2.3.1)
    spring boot:给接口增加签名验证(spring boot 2.3.1)
    intellij idea:设置java方法注释模板(intellij idea 2019.2)
    linux(centos8):zabbix配置邮件报警(监控错误日志)(zabbix5.0)
    linux(centos8):lnmp环境编译安装zabbix5.0
    spring boot:使用多个redis数据源(spring boot 2.3.1)
  • 原文地址:https://www.cnblogs.com/AndyJee/p/4480520.html
Copyright © 2011-2022 走看看