当前位置:   article > 正文

程序员的算法趣题Q08:优秀的扫地机器人_class position { public: int x; int y; }; class ro

class position { public: int x; int y; }; class robot { public: robot(int )

题目

现在市面上有很多扫地机器人,能够为我们分担家务,但是我们很难理解,有时候扫地机器人会重复清扫同一块地方。

  • 假设现在有一款扫地机器人,不会重复扫过的地方,如果它要走四步,在走了第一步后,移动三步有如下9种路径;
  • 第一步可以走4种路径,总共情况有 9*4=36 种路径。
    tZF7Vg.png
    思路:
  • 1.可以(0,0)来表示初始位置,每次可以有四个方向可以选择 上(1,0),下(-1,0),左(0,-1),右(0,1)
  • 2.用一个集合保存所有已经走过的位置,过滤重复走的位置

实现

public class FloormoppingRobot {

    private static int N = 12;
    //移动方向的列表,对应上下左右
    private static List<Position> step = new ArrayList<>(4);;

    static {
        step.add(new Position(0,1));
        step.add(new Position(0,-1));
        step.add(new Position(1,0));
        step.add(new Position(-1,0));
    }

    private static int move(List<Position> historyPos){
        //移动部署为N,则历史列表集合size为4时结束
        if( historyPos.size() == N + 1){
            return 1;
        }

        int cnt = 0;
        for( Position move : step ){
            //取出列表里最后一个元素
            int lastIndex = historyPos.size()-1;
            Position currentPosition = historyPos.get(lastIndex);

            //计算机器人下一步的位置
            Position nextPosition = new Position(currentPosition.x+move.x, currentPosition.y + move.y);

            if( !historyPos.contains(nextPosition) ){
                historyPos.add(nextPosition);
                cnt += move(historyPos);
                historyPos.remove(nextPosition);
            }
        }

        return cnt;
    }

    public static void main(String[] args) {
        List<Position> pos = new ArrayList<>();
        pos.add(new Position(0,0));

        List<Position> historyPos = new ArrayList<>();

        int cnt = move(pos);
        System.out.println(cnt);
    }

}

class Position {
    int x;
    int y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Position position = (Position) o;
        return x == position.x &&
                y == position.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

资源地址

原书代码实现git库:https://github.com/allen1995/70-math-quizs-for-programmers

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/670088
推荐阅读
相关标签
  

闽ICP备14008679号