首页 > tw无人机路线规划代码
头像
是瑶瑶公主呀
编辑于 2020-10-21 09:14
+ 关注

tw无人机路线规划代码

很多私信都问过这个问题 今天清理idea里的代码
分享一下 然后清理了
问题描述:
一个字符型二维矩阵中,每个格子都代表一个地理位置信息,需要设计一个无人机的飞行路线,拍下地图全景,每次可以拍上下左右中五个位置的照片。
主要思路:
1 螺旋飞行+3格定时拍照,每三个格子拍照一次,可以保证拍下全图。足够大的情况下,拍照次数约为全图1/3
2 隔行跳跃飞行,可以保证拍下全图。足够大的情况下,飞行距离约为全图1/3
代码很简单,思路也很简单,这一面一般不会挂人,最后挂在了非技术上也是挺离谱

import java.util.Scanner;

public class Plane {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int row = input.nextInt();
        int col = input.nextInt();
        // 创建地图
        int[][] map = createMap(row, col);
        // 螺旋飞行
        //new SolutionOne().spiralTravel(map, 0, map[0].length - 1, 0, map.length - 1);
        // 跳跃飞行
        new SolutionTwo().skipTravel(map, 0, map[0].length - 1, 1, map.length - 1);
        // 打印结果
        printMap(map);
    }

    private static int[][] createMap(int row, int col) {
        return new int[row][col];
    }

    private static void printMap(int[][] map) {
        for (int[] info : map) {
            for (int val : info) {
                System.out.print(val + " ");
            }
            System.out.println();
        }
    }
}

public class SolutionOne {

    public void spiralTravel(int[][] map, int left, int right, int up, int down) {
        int count = 0;
        while (true) {
            // 左到右
            for (int col = left; col <= right; col++) {
                takePhoto(map, up, col, count++);
            }
            if (++up > down) break;
            // 上到下
            for (int row = up; row <= down; row++) {
                takePhoto(map, row, right, count++);
            }
            if (--right < left) break;
            // 右到左
            for (int col = right; col >= left; col--) {
                takePhoto(map, down, col, count++);
            }
            if (--down < up) break;
            // 下到上
            for (int row = down; row >= up; row--) {
                takePhoto(map, row, left, count++);
            }
            if (++left > right) break;
        }
    }

    private void takePhoto(int[][] map, int row, int col, int count) {
        if (count % 3 == 0) {
            map[row][col] = 1;
        }
    }
}
public class SolutionTwo {

    public void skipTravel(int[][] map, int left, int right, int up, int down) {
        boolean leftToRight = true;
        while (true) {
            if (leftToRight) {
                moveL2R(map, left, right, up);
                leftToRight = false;
            } else {
                moveR2L(map, left, right, up);
                leftToRight = true;
            }
            // 边界判断
            if (up + 1 == down) {
                break;
            }
            // 跳跃条件
            if (up + 3 < down) {
                up = leftToRight ? skip(map, up, left, 3) : skip(map, up, right, 3);
            } else if (up + 3 == down) {
                up = leftToRight ? skip(map, up, left, 2) : skip(map, up, right, 2);
            } else {
                up = leftToRight ? skip(map, up, left, 1) : skip(map, up, right, 1);
            }
        }
    }

    private int skip(int[][] map, int up, int direction, int count) {
        for (int i = 0; i < count; i++) {
            map[up++][direction] = 1;
        }
        return up;
    }


    private void moveL2R(int[][] map, int left, int right, int up) {
        for (int col = left; col <= right; col++) {
            map[up][col] = 1;
        }
    }

    private void moveR2L(int[][] map, int left, int right, int up) {
        for (int col = right; col >= left; col--) {
            map[up][col] = 1;
        }
    }
}

全部评论

(17) 回帖
加载中...
话题 回帖

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐