机器人的运动范围

机器人的运动范围

http://www.nowcoder.com/questionTerminal/6e5207314b5241fb83f2329e89fdecc8

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

  1. 使用BFS模板
    提供最短路径中的BFS模板如下,对于不同的题目需要进行相应的变化,此题目只用计数就行:

    /**
    * Return the length of the shortest path between root and target node.
    */
    int BFS(Node root, Node target) {
     Queue<Node> queue;  // store all nodes which are waiting to be processed
     Set<Node> used;     // store all the used nodes
     int step = 0;       // number of steps neeeded from root to current node
     // initialize
     add root to queue;
     add root to used;
     // BFS
     while (queue is not empty) {
         step = step + 1;
         // iterate the nodes which are already in the queue
         int size = queue.size();
         for (int i = 0; i < size; ++i) {
             Node cur = the first node in queue;
             return step if cur is target;
             for (Node next : the neighbors of cur) {
                 if (next is not in used) {
                     add next to queue;
                     add next to used;
                 }
             }
             remove the first node from queue;
         }
     }
     return -1;          // there is no path from root to target
    }
  2. 注意终止条件以及能进入队列的条件,当路被挡住时,便不能再加入队列进行判断

  3. 坐标从(0,0)开始,可以只走右边和下边就能实现遍历

  4. MMP,我怎么这么笨,IDEA里面调试了好久才搞对😤😤😤

    import java.util.Queue;
    import java.util.LinkedList;
    class position{
     int x;
     int y;
     public position(int x,int y){
         this.x=x;
         this.y=y;
     }
     public int getX(){
         return x;
     }
     public int getY(){
         return y;
     }
    }
    public class Solution {
     public int movingCount(int threshold, int rows, int cols)
     {
         Queue<position> que=new LinkedList<>();
         que.add(new position(0,0));
         boolean[][] aq=new boolean[rows+1][cols+1];
         aq[0][0]=true;
         int num=0;
         while(!que.isEmpty()){
             int size=que.size();
             for(int i=0;i<size;i++){
                 position po=que.poll();
                 if(po.getX()<rows&&po.getY()<cols&&((byteSum(po.getX())+byteSum(po.getY()))<=threshold)){
                     num++;
                     position po1=new position(po.getX()+1,po.getY());
                     position po2=new position(po.getX(),po.getY()+1);
                     if(po1.getX()<rows&&po1.getY()<cols&&aq[po1.getX()][po1.getY()]==false){
                         aq[po1.getX()][po1.getY()]=true;
                         que.add(po1);
                     }
                     if(po2.getX()<rows&&po2.getY()<cols&&aq[po2.getX()][po2.getY()]==false){
                         aq[po2.getX()][po2.getY()]=true;
                         que.add(po2);
                     }
                 }
    
             }
    
         }
         return num;
     }
     private int byteSum(int n){
         int sum=0;
         while(n!=0){
             sum=sum+n%10;
             n=n/10;
         }
         return sum;
     }
    }
全部评论

相关推荐

家人们,我现在真的好纠结。我是26届的,目前还没有实习过。我现在的情况是,想参加秋招,但是感觉自己的简历特别空,没有实习经历会不会秋招直接凉凉啊?可我又听说现在很多公司对26届实习生也不太感冒,说什么不确定性大。而且我最近在准备考公,时间上也有点冲突。要是把时间花在实习上,备考时间就少了。但要是不实习,又怕以后就业有问题😫有没有懂行的友友帮我分析分析:26届现在不实习,秋招找工作真的会很难吗?考公和实习该怎么平衡啊?如果现在不实习,考完公再去找实习还来得及吗?真的太焦虑了,希望大家能给我点建议🙏
小破站_程序员YT:我可能和大家的观点不一样。人的精力是有限的,不能既要还要。你又想实习又想考公最后又要秋招上岸,我觉得哪有那么多的选择。你如果想考上岸,那就全力以赴。如果想秋招上岸,就继续投实习,投没了,就继续准备秋招,秋招不行继续春招。别到最后,考公没上岸,觉得是花了时间浪费在找实习上了, 秋招没上岸,觉得是浪费时间准备考公去了。我是认为很难说可以去平衡 不喜勿喷,可以叫我删除
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
点赞 评论 收藏
分享
06-23 17:45
门头沟学院 Java
里面的项目啥的真的有用吗?&nbsp;这些人是割韭菜吗?
HellowordX:很简单,如果你有自己稳定的学习路线和获取知识的方式就没必要,如果你啥都不懂的小白或者里边有你感兴趣的知识,我觉得挺值,我也经常为知识付费,因为时间精力有限,很多东西我不可能自己重复造轮子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务