「洛谷」P1126 机器人搬重物

P1126 机器人搬重物

https://www.luogu.com.cn/problem/P1126

题目描述

机器人移动学会(RMI)现在正尝试用机器人搬运物品。机器人的形状是一个直径1.61.6米的球。在试验阶段,机器人被用于在一个储藏室中搬运货物。储藏室是一个$ N \times M$ 的网格,有些格子为不可移动的障碍。机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方。机器人接受的指令有:向前移动1步(Creep);向前移动2步(Walk);向前移动3 步(Run);向左转(Left);向右转(Right)。每个指令所需要的时间为1 秒。请你计算一下机器人完成任务所需的最少时间。

输入描述

第一行为两个正整数 N , M ( N , M ≤ 50 ) N,M(N,M \le 50) N,M(N,M50),下面N行是储藏室的构造,0表示无障碍,1表示有障碍,数字之间用一个空格隔开。接着一行有4个整数和1个大写字母,分别为起始点和目标点左上角网格的行与列,起始时的面对方向(东E,南S,西W,北N),数与数,数与字母之间均用一个空格隔开。终点的面向方向是任意的。

输出描述

一个整数,表示机器人完成任务所需的最少时间。如果无法到达,输出 −1。

在这里插入图片描述

样例

#1
9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 S
12

提示

解析

需要注意:

  1. 题目要求的不是我们平常的网格走法,而是格点走法。
  2. 机器人本身就有宽度大小,若触碰到障碍物,则不能走。
  3. 机器人指令每次都消耗一个时间点。
  4. 网格周围边界不可以走。

在这里插入图片描述

代码方面:

使用数组 d x , d y dx, dy dx,dy 顺时针来表示 北、东、南、西 四个方向的行走趋势,因为不管方向如何,都是走三步,因此 × k \times k ×k 就行。

数组 A A A 用来标记障碍物,数组 v i s vis vis 用来统计最优时间。

然后 对某个点的四个方向三个走法进行 BFS ,每次都有 12 种状态,即四个方向对应的三步,只取最优的。

另外,注意一下如下问题:

  • 起点和终点重复
  • 没有起点或终点
    在这里插入图片描述

一定要注意,四个方向必须最好按照 北、东、南、西 顺时针这样的顺序,图中左边为顺时针,而右边是上北下南左西右东的顺序,我就WA在这里了。

AC Code

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    static final int INF = 0xfffff;
    static final int MAX = 55;
    static boolean[][] A = new boolean[MAX][MAX];
    static int[][] vis = new int[MAX][MAX];
    static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1}; // 北东南西 必须顺时针
    static int[] dx2 = {0, 0, 1, 1}, dy2 = {0, 1, 1, 0};
    static int n, m;
    static int sx, sy, tx, ty, dir;

    static final int N = 0; // 北
    static final int E = 1; // 东
    static final int S = 2; // 南
    static final int W = 3; // 西

    public static void main(String[] args) throws Exception {
        n = nextInt(); m = nextInt();
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) A[i][j] = nextInt() == 1;
        }

        for(int i = 0; i <= n; i++)
            Arrays.fill(vis[i], INF);

        sx = nextInt(); sy = nextInt();
        tx = nextInt(); ty = nextInt();
        dir = br.read();

        initDir();

        Deque<Node> queue = new LinkedList<>();
        queue.addLast(new Node(sx, sy, 0, dir));
        vis[sx][sy] = 0;

        while(!queue.isEmpty()) {
            Node cur = queue.removeFirst();
            for(int i = 0; i < 4; i++) { // 北东南西 必须顺时针
                for(int k = 1; k <= 3; k++) {
                    int nx = cur.x + dx[i] * k, ny = cur.y + dy[i] * k;
                    if(!check(nx, ny)) break; // 若不满足,直接退出,因为一大步取决于前面的一小步
                    int temp = cur.step + 1; // +1 是因为三步距离都算 1 个时间,且必然产生移动
                    int val = Math.abs(cur.dir - i); // 计算从 cur.dir 转向到 i 顺时间需要多少步
                    if(val == 1 || val == 3) temp += 1;
                    else if(val == 2) temp += 2;
                    if(temp >= vis[nx][ny]) continue; // 我们只需要最小的,类似最短路吧
                    vis[nx][ny] = temp;
                    Node next = new Node(nx, ny, temp, i);
                    queue.addLast(next);
                }
            }
        }
        if(vis[tx][ty] != INF) System.out.println(vis[tx][ty]);
        else System.out.println(-1);
    }

    public static boolean check(int x, int y) {
        for(int i = 0; i < 4; i++) {
            int nx = x + dx2[i], ny = y + dy2[i];
            if(x <= 0 || x >= n || y <= 0 || y >= m) return false;
            if(A[nx][ny]) return false;
        }
        return true;
    }

    public static void initDir() {
        if(dir == 'N') dir = N;
        else if(dir == 'S') dir = S;
        else if(dir == 'W') dir = W;
        else if(dir == 'E') dir = E;
    }

    public static int nextInt() throws Exception {
        st.nextToken();
        return (int) st.nval;
    }
}

class Node {
    int x, y, step, dir;
    public Node(int x, int y, int step, int dir) {
        this.x = x;
        this.y = y;
        this.step = step;
        this.dir = dir;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值