Find a way
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.
InputThe 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
OutputFor 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
1 import java.util.ArrayDeque; 2 import java.util.PriorityQueue; 3 import java.util.Scanner; 4 class node{ 5 int x; 6 int y; 7 int step; 8 int flag; 9 } 10 public class Main { 11 static int n,m; 12 static int [][][] vis = new int[220][220][2]; 13 static char[][] c = new char[220][220]; 14 static int [][][] dis = new int[220][220][2]; 15 static String[] s = new String[220]; 16 static int [][] dir = {{1,0},{0,1},{-1,0},{0,-1}}; 17 static node head,tail; 18 static ArrayDeque<node> q = new ArrayDeque<node>(); 19 static void bfs() { 20 while(!q.isEmpty()) { 21 head = q.poll(); 22 //System.out.println(head.x+" "+head.y+" "+head.flag); 23 //System.out.println("lallaa"+vis[head.x][head.y][head.flag]); 24 for(int i=0;i<4;i++) { 25 int tx = head.x+dir[i][0]; 26 int ty = head.y+dir[i][1]; 27 int tstep = head.step + 1; 28 if(tx<0||tx>=n||ty<0||ty>=m||c[tx][ty]=='#'||vis[tx][ty][head.flag]==1) 29 continue; 30 if(c[tx][ty]=='@') { 31 if(head.flag==0) { 32 // System.out.println("haah"); 33 dis[tx][ty][0] = Math.min(dis[tx][ty][0], tstep); 34 vis[tx][ty][0] = 1; 35 } 36 else if(head.flag==1) { 37 dis[tx][ty][1] = Math.min(dis[tx][ty][1], tstep); 38 vis[tx][ty][1] = 1; 39 } 40 } 41 vis[tx][ty][head.flag] = 1; 42 tail = new node(); 43 tail.flag = head.flag; 44 tail.step = head.step + 1; 45 tail.x = tx; 46 tail.y = ty; 47 q.offer(tail); 48 } 49 } 50 } 51 public static void main(String[] args) { 52 Scanner cin = new Scanner(System.in); 53 while(cin.hasNext()) { 54 n = cin.nextInt(); 55 m = cin.nextInt(); 56 for(int i=0;i<n;i++) { 57 for(int j=0;j<m;j++) { 58 for(int k=0;k<2;k++) { 59 dis[i][j][k] = 1000000; 60 } 61 } 62 } 63 for(int i=0;i<n;i++) { 64 for(int j=0;j<m;j++) { 65 for(int k=0;k<2;k++) { 66 vis[i][j][k] = 0; 67 } 68 } 69 } 70 for(int i=0;i<n;i++) { 71 s[i] = cin.next(); 72 } 73 for(int i=0;i<n;i++) { 74 for(int j=0;j<s[i].length();j++) { 75 c[i][j] = s[i].charAt(j); 76 if(c[i][j] == 'Y') { 77 head = new node(); 78 head.step = 0; 79 head.flag = 0; 80 head.x = i; 81 head.y = j; 82 q.offer(head); 83 vis[i][j][0] = 1; 84 //bfs(); 85 } 86 if(c[i][j] == 'M') { 87 head = new node(); 88 head.step = 0; 89 head.flag = 1; 90 head.x = i; 91 head.y = j; 92 q.offer(head); 93 vis[i][j][1] = 1; 94 95 } 96 } 97 } 98 bfs(); 99 int ans = 1000000; 100 for(int i=0;i<n;i++) { 101 for(int j=0;j<m;j++) { 102 if(c[i][j] == '@') { 103 ans = Math.min(ans,dis[i][j][0]+dis[i][j][1]); 104 } 105 } 106 } 107 System.out.println(ans*11); 108 } 109 } 110 }