java写bfs
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java写bfs,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5079字,纯文字阅读大概需要8分钟。
内容图文
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 }
内容总结
以上是互联网集市为您收集整理的java写bfs全部内容,希望文章能够帮你解决java写bfs所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。