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.
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.
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
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
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;}}
阅读全文
0 0
- Find a way -从两个点分别bfs
- 【hdu】 Find a way (分别从两个点出发BFS,再计算最小值)
- Find a way(BFS)
- K - Find a way(BFS)
- Find a way(bfs)
- [bfs]HDU2612 Find a way
- BFS-E - Find a way
- HDU2612 Find a way BFS
- hdoj2612 Find a Way BFS
- Find a way(bfs)
- BFS:HDU2612-Find a way(双向BFS)
- HDU2612(Find a way)搜索-两次BFS
- hdu 2612 Find a way(BFS)
- HDU 2612 Find a way(两次BFS)
- hdoj 2612 Find a way (BFS)
- HDU 2612 Find a way(BFS)
- HDU 2612 Find a way BFS
- Hdu 2612 Find a way(bfs)
- 曹可爱之最可爱-Vue.js入门(四)计算属性和观察者
- spring基于AspectJ的AOP编程
- 学习阿里巴巴开发手册-9
- 3秒钟不懂你砍我:一行代码解决ListView,GridView的一切
- java 笔试常考零碎知识点
- Find a way -从两个点分别bfs
- JAVA多维Map
- 头疼,技术氛围究竟怎么来
- HashMap,HashTable和ConcurrentHashMap的区别
- PHP攻城狮培养计划-PHP进阶:面向对象及TP5框架初识
- Installing TensorFlow on Ubuntu 1.3
- 图像处理技术上的空间域和空间频率域
- linux性能调优命令
- 从GoogLeNet架构到deep dream模型