博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java写bfs
阅读量:4345 次
发布时间:2019-06-07

本文共 3340 字,大约阅读时间需要 11 分钟。

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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#

Sample Output

668866
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
q = new ArrayDeque
(); 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

 

转载于:https://www.cnblogs.com/1013star/p/10353589.html

你可能感兴趣的文章
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>