计蒜客:蒜头君回家(java)

运用bfs算法

蒜头君要回家,但是他家的钥匙在他的朋友花椰妹手里,他要先从花椰妹手里取得钥匙才能回到家。花椰妹告诉他:“你家的钥匙被我复制了很多个,分别放在不同的地方。”

蒜头君希望能尽快回到家中,他需要首先取得任意一把钥匙,请你帮他计算出回家所需要的最短路程。

蒜头君生活的城市可以看做是一个n×m的网格,其中有道路有障碍,钥匙和家所在的地方可以看做是道路,可以通过。蒜头君可以在城市中沿着上下左右4个方向移动,移动一个格子算做走一步。

输入格式

第一行有两个整数nm。城市的地图是nm列。(1≤n,m≤2000)

接下来的n行,每行m个字符,代表城市的地图。'.' 代表道路,'#' 代表障碍物,'S' 代表蒜头君所在的位置,'T' 代表蒜头家的位置,'P'代表钥匙的位置。除了障碍物以外,别的地方都可以通过。(题目保证蒜头君至少有一条路径可以顺利拿到钥匙并且回家)

输出格式

输出蒜头回家要走的最少步数,占一行。

样例输入

8 10

P.####.#P#

..#..#...#

..#T##.#.#

..........

..##.#####

..........

#####...##

###....S##

样例输出

21

// 蒜头君回家
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static class Node {
        int x;
        int y;
        int step;    
        int key = 0;
        public Node(int x, int y, int step, int key) {
            this.x = x;
            this.y = y;
            this.step = step;
            this.key = key;
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int sx = 0;
        int sy = 0;
        char[][] map = new char[n][m];
        int[][][] vis = new int[n + 5][m + 5][2];
        int[][] dir = { {-1, 0},{0, 1},{1, 0},{0, -1} };
        for (int i = 0; i < n; i++) {
            String str = in.next();
            for (int j = 0; j < m; j++) {
            	map[i][j] = str.charAt(j);
                if(map[i][j] == 'S') {
                    sx = i;
                    sy = j;
                }
            }
        }
        Node start = new Node(sx, sy, 0, 0);
        Queue<Node> queue = new LinkedList<Node>();
        queue.add(start);
        Node now = start;
        vis[sx][sy][0] = 1;
        while(!queue.isEmpty()) {
            now = queue.poll();
            int x = now.x;
            int y = now.y;
            int step = now.step;
            int key = now.key;
            if(map[x][y] == 'P') {
                key = 1;
                vis[x][y][1] = 1;
            }
            if(map[x][y] == 'T' && key == 1) {
                System.out.println(now.step);
                return;
            }
            for (int i = 0; i < 4; i++) {
                int tx = x + dir[i][0];
                int ty = y + dir[i][1];
                if(tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
                if(map[tx][ty] != '#' && vis[tx][ty][key] == 0) {
                    vis[tx][ty][key] = 1;
                    Node next = new Node(tx, ty, step + 1, key);
                    queue.add(next);
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光搬运工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值