Find a way(java版)

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. 
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. 

Input

The input contains multiple test cases. 
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character. 
‘Y’ express yifenfei initial position. 
‘M’    express Merceki initial position. 
‘#’ forbid road; 
‘.’ Road. 
‘@’ KCF 

Output

For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.

Sample Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

用一个三维数组去标记,注意求的是两个人在路上用的总时间

import java.util.*;
public class Main {
    static int n,m;
    static String[] map;
    static int[][][] vis;
    static int[][] move = {{0,1},{0,-1},{1,0},{-1,0}};
    static Queue<Node> q;
    
    static int bfs(Node a,Node b) {
        q.add(a);q.add(b);
        int res = Integer.MAX_VALUE;
        vis[a.x][a.y][a.op] = 1;vis[b.x][b.y][b.op] = 1;
        Node cur;
        while(!q.isEmpty()) {
            cur = new Node(q.peek().x,q.peek().y,q.poll().op);
            if(map[cur.x].charAt(cur.y) == '@' && vis[cur.x][cur.y][0] != 0 && vis[cur.x][cur.y][1] != 0)
                res = Math.min(res,vis[cur.x][cur.y][0] + vis[cur.x][cur.y][1]);
            for(int i = 0;i < 4;i++) {
                Node new_cur = new Node(cur.x + move[i][0],cur.y + move[i][1],cur.op);
                if(check(new_cur.x,new_cur.y,new_cur.op)) {
                    vis[new_cur.x][new_cur.y][new_cur.op] = vis[cur.x][cur.y][cur.op] + 1;
                    q.add(new_cur);
                }
            }
        }
        return res - 2;
    }
    
    static boolean check(int x,int y,int i) {
        return x >= 0 && x < n && y >= 0 && y < m  && map[x].charAt(y) != '#' && vis[x][y][i] == 0;
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            q = new LinkedList<Node>();
            n = cin.nextInt();
            m = cin.nextInt();
            vis = new int[205][205][2];
            map = new String[205];
            Node a = new Node();
            Node b = new Node();
            for(int i = 0;i < n;i++) {
                map[i] = cin.next();
                for(int j = 0;j < m;j++) {
                    if(map[i].charAt(j) == 'Y') 
                        a = new Node(i,j,0);
                    if(map[i].charAt(j) == 'M') 
                        b = new Node(i,j,1);
                }
            }
            int ans = bfs(a,b);
            System.out.println(ans * 11);
        }
    }
}
class Node {
    int x,y,op;//op等于0代表y,op等于1代表m
    Node(){}
    Node(int x,int y,int op) {
        this.x = x;
        this.y = y;
        this.op = op;
    }
}

 

全部评论

相关推荐

03-29 17:05
门头沟学院 Java
asdasdasda...:我前段时间找工作焦虑,有几天连续熬夜熬穿了,然后心脏突然不舒服,立马躺床上睡觉了,然后第二天还是不舒服,去看医生说是心率不齐,吓得我后面天天早早睡觉,调养身体,过了好几天才好过来。所以真的,工作这些东西哪有那么重要,最多钱多一点钱少一点,降低物欲。活着才是最重要的,现在想想真的后怕
如何排解工作中的焦虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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