Find a way -从两个点分别bfs

来源:互联网 发布:ubuntu hadoop 2.6 编辑:程序博客网 时间:2024/05/22 05:31
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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866
分析:从两个点分别bfs即可,只是对于要达到同一个终点的处理有点棘手,这里是采用的两个数组来分别记录.bfs由于是一层一层扩展的,那么只要达到便是最优的,所以不需要回溯!
import java.util.*;public class Main {static Scanner in = new Scanner(System.in);static int m, n, sum,min;static char[][] matrix = new char[205][205];static boolean[][] vis = new boolean[205][205];static int[][] sp1 = new int[205][205];static int[][] sp2 = new int[205][205];static des mm,yy;static int[][] dir = {  { 0, 1 },  { 1, 0 },  { 0, -1 },  { -1, 0 } };static void bfs(des d,int[][] step) {Queue<des> q  = new LinkedList<des>();   q.add(d);while(!q.isEmpty()) {  des temp = q.poll(); for (int i = 0; i < 4; i++) {  des p = new des();   p.x = temp.x + dir[i][0];   p.y = temp.y + dir[i][1];  if (p.x >= n || p.y >= m|| p.x < 0 || p.y < 0)continue;  if((matrix[p.x][p.y]=='.'||matrix[p.x][p.y]=='@')&&vis[p.x][p.y]==false) {    vis[p.x][p.y]=true;   step[p.x][p.y]=step[temp.x][temp.y]+1;   q.add(p);  }      }  }}public static void main(String[] args) {while(in.hasNext()){int min=Integer.MAX_VALUE;n = in.nextInt();m = in.nextInt();if(m==0&&n==0)break;String s="";for (int i = 0; i < n; i++) { s=in.next();for (int j = 0; j < s.length(); j++) {matrix[i][j] = s.charAt(j); vis[i][j]=false; sp1[i][j]=0; sp2[i][j]=0;if(matrix[i][j]=='Y'){ yy = new des(i, j);vis[i][j]=true;} if(matrix[i][j]=='M'){    mm = new des(i, j);vis[i][j]=true;}}}bfs(yy,sp1);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {vis[i][j]=false;//System.out.println(sp1[i][j]);}}bfs(mm,sp2);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {//System.out.println(sp1[i][j]+" "+sp2[i][j]);} }for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if(matrix[i][j]=='@'&&sp1[i][j]!=0&&sp2[i][j]!=0) {if((sp1[i][j]+sp2[i][j])<min) min=(sp1[i][j]+sp2[i][j]);}}}      System.out.println(min*11);      }}}class des{int x;int y;des(){}des(int x,int y){this.x=x;this.y=y;}}


原创粉丝点击