Pocket Gem OA: Path Finder
1. 有向图 找所有start node到end node之间的路径输入是一个txt 形式如下:A EA : B C D. B : CC : ED : B. 输出一个List<String> 是从A到E所有的path
题不难,主要是注意STDIN
1 package pocketGems; 2 3 import java.io.*; 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.HashMap; 7 import java.util.HashSet; 8 import java.util.Set; 9 10 public class PathFinder2 { 11 public static void main(String[] args) 12 throws FileNotFoundException, IOException { 13 String filename = "C:/Users/yang liu/workspace/Interview2017/src/pocketGems/input_1.txt"; 14 if (args.length > 0) { 15 filename = args[0]; 16 } 17 18 19 List<String> answer = parseFile(filename); 20 System.out.println(answer); 21 } 22 23 static List<String> parseFile(String filename) 24 throws FileNotFoundException, IOException { 25 /* 26 * Don't modify this function 27 */ 28 BufferedReader input = new BufferedReader(new FileReader(filename)); 29 List<String> allLines = new ArrayList<String>(); 30 String line; 31 while ((line = input.readLine()) != null) { 32 allLines.add(line); 33 } 34 input.close(); 35 36 37 return parseLines(allLines); 38 } 39 40 static List<String> parseLines(List<String> allLines) { 41 HashMap<String, HashSet<String>> graph = new HashMap<String, HashSet<String>>(); 42 Set<String> visited = new HashSet<String>(); 43 String src = allLines.get(0).split(" ")[0]; 44 String dst = allLines.get(0).split(" ")[1]; 45 for (int i=1; i<allLines.size(); i++) { 46 String line = allLines.get(i); 47 String node = line.trim().split(":")[0].trim(); 48 String[] neibors = line.split(":")[1].trim().split(" "); 49 graph.put(node, new HashSet<String>()); 50 for (String nb : neibors) { 51 if (nb.length() != 0) graph.get(node).add(nb); 52 } 53 } 54 55 List<String> res = new ArrayList<String>(); 56 visited.add(src); 57 helper(res, src, src, dst, visited, graph); 58 return res; 59 } 60 61 static void helper(List<String> res, String path, String cur, String dst, Set<String> visited, HashMap<String, HashSet<String>> graph) { 62 if (cur.equals(dst)) { 63 res.add(path); 64 return; 65 } 66 HashSet<String> neibors = graph.get(cur); 67 if (neibors != null) { 68 for (String each : neibors) { 69 if (visited.contains(each)) continue; 70 visited.add(each); 71 helper(res, path+each, each, dst, visited, graph); 72 visited.remove(each); 73 } 74 } 75 } 76 }
Pocket Gem OA: Path Finder相关推荐
- 3kyu Path Finder #3: the Alpinist
3kyu Path Finder #3: the Alpinist 题目背景: Task You are at start location [0, 0] in mountain area of Nx ...
- 4kyu Path Finder #2: shortest path
4kyu Path Finder #2: shortest path 题目背景: Task You are at position [0, 0] in maze NxN and you can onl ...
- 4kyu Path Finder #1: can you reach the exit?
4kyu Path Finder #1: can you reach the exit? 题目背景: Task You are at position [0, 0] in maze NxN and y ...
- mac必备的文件管理器:Path Finder Mac中文版
文件管理器当然要选Path Finder Mac中文版,让你管理文件更加轻松,Path Finder for Mac中文版提供了双窗格视图.查看和操作隐藏的文件.文件夹比较和同步.文件夹合并.批量重命 ...
- Path Finder for Mac(Mac文件管理工具)
path finder for mac是Mac平台上一款能够让您完全控制文件的Mac文件管理工具.通过path finder for mac你可以按照自己的要求进行文件的排序,还可以直接显示隐藏文件, ...
- Mac文件管理工具:Path Finder
Path Finder是一款Mac平台上的文件管理和操作工具,提供了比Finder更丰富的功能和更直观的用户界面.它可以帮助用户更高效地浏览.复制.移动.删除和管理文件,以及进行各种高级操作. Pat ...
- Path Finder for Mac(系统文件管理器)
Path Finder 9 Mac中文特别版是一款功能非常强大,十分优秀的系统文件管理器,Path Finder mac能够轻而易举访问系统文件或者网络硬盘.使用起来比系统自带的文件管理器简单方便多了 ...
- 文件管理工具:Path Finder mac
Path Finder mac是Mac上好用的文件管理工具,是一个用户友好的应用程序,可以比较和同步文件夹,查看隐藏文件,使用双窗格和全键盘导航来浏览文件系统,以节省您的时间,端的自定义允许您以许多不 ...
- jpf java 是什么_[Java Path Finder][JPF学习笔记][6]jpf-symbc的初步使用
这篇日志继续总结些简单的JPF使用经验: 这里摘录一些设置,进行简单的中文解释: # The following JPF options are usually used for SPF as wel ...
最新文章
- linux系统中find怎么用,linux系统中‘find’的详细用法
- java解析getresponsebodyasstring_java读取网站内容的两种方法是什么呢?
- 50万数据生成6位数不重复字符串_JAVA技术分享:单号的生成
- 决策类AI成最具商业价值应用产品 第四范式入选Gartner中国5大代表厂商
- ITK:在签名图像中找到零交叉点
- 一轮项目冲刺——移山小分队(2)
- 柴油机计算机故障,柴油机故障论文,关于6105小型高速柴油机常见故障处理相关参考文献资料-免费论文范文...
- 通过rtcwake命令设置系统S3(休眠到内存)/S4(挂起到硬盘)一段时间后自动唤醒
- Fiddler之为什么我没有抓到网络请求的js链接
- [原创]利用DropDownlist来控制Textbox输入数字的精度
- php压缩中文乱码,完美解决PHP中文乱码
- js中数组的操作方法
- .view-content:overflow:hidden 大错误
- UInput使用注意事项
- 在lua的string库和正则表达式
- SLAM学习笔记-------------(四)李群与李代数
- 无线网络dns服务器被劫持,dns劫持,教您dns被劫持了怎么解决
- 微信小程序转发功能的实现
- 如何让Loadrunner或Jmeter发送邮件报告
- 导入的素材PS突然很卡,但是内存足够、素材图层也不多。。。该怎么办呢????
热门文章
- Python中functools模块函数解析
- 转 Caffe学习系列(4):激活层(Activiation Layers)及参数
- Android学习笔记(八)
- [案例分析] 打造值得信任的个人品牌究竟靠什么?
- aurora 初学页面元素
- html嵌入excel_第5天 | 16天搞定前端,html布局,表格和大块头
- 首先请与所有现有链接到该网络共享的映射断开连接_疫情之下:该如何使用Python预测员工流失,老板直呼内行!...
- php打印预览jquery,php 实现打印预览的功能
- tensorflow-yolo3系列配置文章汇总
- 输入三个字符串,按由小到大的顺序输出 字符串排序-一种简单的方法