记ThoughtWorks一道HomeWork
The local commuter railroad services a number of towns in Kiwiland.  Because of monetary concerns, all of the tracks are 'one-way.'  That is, a route from Kaitaia to Invercargill does not imply the existence of a route from Invercargill to Kaitaia.  In fact, even if both of these routes do happen to exist, they are distinct and are not necessarily the same distance!
 
The purpose of this problem is to help the railroad provide its customers with information about the routes.  In particular, you will compute the distance along a certain route, the number of different routes between two towns, and the shortest route between two towns.
 
Input:  A directed graph where a node represents a town and an edge represents a route between two towns.  The weighting of the edge represents the distance between the two towns.  A given route will never appear more than once, and for a given route, the starting and ending town will not be the same town.
 
Output: For test input 1 through 5, if no such route exists, output 'NO SUCH ROUTE'.  Otherwise, follow the route as given; do not make any extra stops!  For example, the first problem means to start at city A, then travel directly to city B (a distance of 5), then directly to city C (a distance of 4).
  1. The distance of the route A-B-C.
  2. The distance of the route A-D.
  3. The distance of the route A-D-C.
  4. The distance of the route A-E-B-C-D.
  5. The distance of the route A-E-D.
  6. The number of trips starting at C and ending at C with a maximum of 3 stops.  In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops).
  7. The number of trips starting at A and ending at C with exactly 4 stops.  In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D,E,B).
  8. The length of the shortest route (in terms of distance to travel) from A to C.
  9. The length of the shortest route (in terms of distance to travel) from B to B.
  10. The number of different routes from C to C with a distance of less than 30.  In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC.
 
Test Input:
For the test input, the towns are named using the first few letters of the alphabet from A to D.  A route between two towns (A to B) with a distance of 5 is represented as AB5.
Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7
Expected Output:
Output #1: 9
Output #2: 5
Output #3: 13
Output #4: 22
Output #5: NO SUCH ROUTE
Output #6: 2
Output #7: 3
Output #8: 9
Output #9: 9
Output #10: 7
public class HomeWork {/**
     * 使用起始站和终点站位节点创建一个二维数组
     */
    private static int[][] arrs = {{0,5,0,5,7},
            {0,0,4,0,0},
            {0,0,0,8,2},
            {0,0,8,0,6},
            {0,3,0,0,0}};

    /**
     * 最短路径
     */
    private static String shortedPath = "";

    /**
     * 最短路径值
     */
    private static int shortedVal = Integer.MAX_VALUE;

    public static void main(String[] args){List<String> list1 = new ArrayList<String>();
        list1.add("A");
        list1.add("B");
        list1.add("C");
        String result1 = HomeWork.getRoute(list1);//Test1
        System.out.println(result1);

        List<String> list2 = new ArrayList<String>();
        list2.add("A");
        list2.add("D");
        String result2 = HomeWork.getRoute(list2);//Test2
        System.out.println(result2);

        List<String> list3 = new ArrayList<String>();
        list3.add("A");
        list3.add("D");
        list3.add("C");
        String result3 = HomeWork.getRoute(list3);//Test3
        System.out.println(result3);

        List<String> list4 = new ArrayList<String>();
        list4.add("A");
        list4.add("E");
        list4.add("B");
        list4.add("C");
        list4.add("D");
        String result4 = HomeWork.getRoute(list4);//Test4
        System.out.println(result4);

        List<String> list5 = new ArrayList<String>();
        list5.add("A");
        list5.add("E");
        list5.add("D");
        String result5 = HomeWork.getRoute(list5);//Test5
        System.out.println(result5);

        List<String> list6 = new ArrayList<>();
        List<String> pathList6 = HomeWork.cTocLessThen("C", "C", 3, list6);//Test6
        System.out.println(pathList6.size());

        int result7 = HomeWork.aTocWith("A", "C", 4);//Test7
        System.out.println(result7);

        int result8 = HomeWork.aTocShorted("A", "C", 0);//Test8
        System.out.println(result8);

        int result9 = HomeWork.aTocShorted("B", "B", 0);//Test9
        System.out.println(result9);

        List<String> list10 = new ArrayList<>();
        List<String> pathList10 = HomeWork.cTocValLessThen("C", "C", 0,list10);//Test10
        System.out.println(pathList10.size());
    }/**
     * Test1.The distance of the route A-B-C.
     * Test2.The distance of the route A-D.
     * Test3.The distance of the route A-D-C.
     * Test4.The distance of the route A-E-B-C-D.
     * Test5.The distance of the route A-E-D.
     */
    private static String getRoute(List<String> list){int result = 0;
        String resultStr = "";
        for (int i = 0; i < list.size() - 1; i++){int m = strToInt(list.get(i));
            int n = strToInt(list.get(i + 1));
            int[] arr = arrs[m];
            int path = arr[n];
            if (path > 0){result += path;
            } else {return "NO SUCH ROUTE";
            }}if (result > 0){resultStr = result + "";
        }else {resultStr = "NO SUCH ROUTE";
        }return resultStr;
    }/**
     * Test6 The number of trips starting at C and ending at C with a maximum of 3 stops.  In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops).
     * @param start 起点
     * @param path 路径
     * @param maxPath 最大步数
     * @param list 路径列表
     * @return
     */
    private static List<String> cTocLessThen(String start, String path, int maxPath,List<String> list) {if (path.length() - 1 > maxPath){return list;
        }if (path.length() > 1 && path.endsWith(start)) {
//            System.out.println(path);
            list.add(path);
        }char lastChar = path.charAt(path.length()-1);
        int m = lastChar - 'A';
        for (int i = 0; i < arrs[m].length; i++){char ch = (char) (i + 'A');
            if (arrs[m][i] > 0){cTocLessThen(start,path + ch,maxPath,list);
            }}return list;
    }/**
     * Test7. The number of trips starting at A and ending at C with exactly 4 stops.  In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D,E,B).
     * @param start 起点
     * @param end 终点
     * @param step 步数
     * @return
     */
    private static int aTocWith(String start,String end,int step){int result = 0;
        String lastRoute = start;
        for (int i = 0; i < step; i++){String route = "";
            for (int j = 0; j < lastRoute.length(); j++){char ch = lastRoute.charAt(j);
                int m = ch - 'A';

                for (int n = 0; n < arrs[m].length; n++){if (arrs[m][n] > 0){route = route + (char)(n + 'A');
                    }}}lastRoute = route;
        }result = lastRoute.split(end).length - 1;
        return result;
    }/**
     * 8. The length of the shortest route (in terms of distance to travel) from A to C.
     * 9. The length of the shortest route (in terms of distance to travel) from B to B.
     * @param path path为起点的路径
     * @param end 终点
     * @param pathVal 路径的值
     * @return
     */
    private static int aTocShorted(String path,String end,int pathVal){if (path.endsWith(end) && pathVal < shortedVal && pathVal > 0) {shortedPath = path;
            shortedVal = pathVal;
            return pathVal;
        }char lastChar = path.charAt(path.length() - 1);
        int m = lastChar - 'A';
        for (int i = 0; i < arrs[m].length; i++){char ch = (char) (i + 'A');
            int value = arrs[m][i];
            if (value > 0){if (path.indexOf(ch) > 0){continue;
                }else {aTocShorted(path + ch,end,pathVal + value);
                }}}return shortedVal;
    }/**
     * 10. The number of different routes from C to C with a distance of less than 30.  In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC.
     * @param path 路径
     * @param end 终点
     * @param pathVal 路径值
     * @param list 路径列表
     * @return
     */
    private static List<String> cTocValLessThen(String path,String end,int pathVal,List<String> list) {if (pathVal >= 30){return list;
        }if (pathVal > 0 && path.endsWith(end)) {
//            System.out.println(path);
            list.add(path);
        }char lastChar = path.charAt(path.length() - 1);
        int m = lastChar - 'A';
        for (int i = 0; i < arrs[m].length; i++) {char newChar = (char) (i + 'A');
            int newCost = arrs[m][i];
            if (newCost > 0) {cTocValLessThen(path + newChar, end, pathVal + newCost,list);
            }}return list;
    }/**
     * 将字符串转换成ASCII对应的数字获取二维数组坐标
     * @param str
     * @return
     */
    public static int strToInt(String str){char[] chars = str.toCharArray();
        int i = 0;
        for (char ch : chars){i = ch - 'A';
        }return i;
    }
}

ThoughtWorks HomeWork相关推荐

  1. 记ThoughtWorks面试失败之旅

    记ThoughtWorks失败之旅 2012年3月3日面试归来,在一次IT求职交流群讨论的时候,无意说了自己去ThoughtWorks面试失败了,顿时引来大批群众围观,问的问题实在太多了,看来大家都对 ...

  2. 记Thoughtworks一次糟糕的面试

    Boss直聘上投了TW,三月某个周二的下午,TW电话过来.问一下离职原因,我说目前业务方向不对.HR介绍了一个TW,问从哪些途径了解TW等.谈到TW的分享文化,我表示我在团队里面也经常分享.这样,你情 ...

  3. 聊聊ThoughtWorks面试(郑大版 社招)+ PS:应届生简单流程介绍

    聊聊ThoughtWorks面试 Tag: ThoughtWorks  面试 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://dreamhead.blogbus.com ...

  4. Travel to ThoughtWorks

    Life is a journey, not a destination. 人生就是在时光的隧道中不同的奔跑,在不同的环境中遇到不同的人,然后开始相同的挣扎,最后不断优秀的过程.加入ThoughtWo ...

  5. 思特沃克(ThoughtWorks) 外企面试题 面试经验分析 面试题 软件测试工程师 测试面试题分享 测试需求理解和用例设计编写 SeleniumUI自动化测试 接口测试 性能测试 实战之战损版

    文章目录 一.前言 二.面试问题 1.面试题问题汇总 展示 2.Automation Testing Homework V2.pdf 2.1 自动化家庭 作业 2.2 自动化家庭作业 Selenium ...

  6. thoughtworks 笔试题及答案

    我自己做的火车题目答案,供大家参考 火车题目答案:https://download.csdn.net/download/xiananliu/10477156 Hi, It was great spea ...

  7. 第一次面试——ThoughtWorks

    都说 ThoughtWorks 是世界最难面试的 IT 公司之一,不是第一 ,就是第二.所以能去 ThoughtWorks 面试也是一次很不错的体验. 之前一直以为自己面试挂了,总是在默默反思,而从未 ...

  8. 记ThoughtWorks一次面试经历

    最近刚面试完ThoughtWorks android开发工程师,趁此机会记录一下,方面后续回顾. 面试流程 电话面试20分钟 由HR电话沟通一些基本情况, Homework 邮件通知(全英文)有一道a ...

  9. ThoughtWorks雷达上的新奇变化

    与往常一样,ThoughtWorks技术雷达涵盖了四方面内容:语言与框架.平台.技术和工具,而且其中每个领域都会有四方面内容:采用.试用.评估及保留.本文列举了相关领域中较新和值得注意的内容. \\ ...

  10. comsat java_java-com.thoughtworks.xstream.converters.ConversionExce...

    [EDITED] 我正在处理的项目是Java J2EE中的3个文件夹项目,带有servlet和Hibernate以实现持久性.结构如下:-管理员->具有bean和HTML / CSS的主程序-J ...

最新文章

  1. centos7 安装kubernetes1.4(kubeadm版)
  2. 全面优化—配置高性能lnmp架构
  3. vue分页tbale小荔枝
  4. Module(模块)
  5. redis 源码 zmalloc.c 实现
  6. [SPS2010] 使用心得 7 - ebook for Installation
  7. linux查看用户的操作记录,Linux下查看用户登陆后的操作记录
  8. java数组元素是类_Java数组及其常用类
  9. 每个Wi-Fi都有独一无二的IP地址吗?
  10. [转]linux signal
  11. 扩展二叉树 (根据特殊的前序遍历建树)
  12. 基于Android Studio的游戏开发-横版格斗.part
  13. uni-app 开发微信小程序定位
  14. 计算机显示器一半有阴影,[显示器图标有影子怎么解决]电脑显示器有字迹影子...
  15. opengl——贴图
  16. LDT面试:实验室开发诊断试剂监管模式(Laboratory Developed Test,LDT)
  17. html5 新增input类型,html5新增的input类型
  18. AES解密报错,Input length must be multiple of 16 when decrypting with padded cipher
  19. 国内的IT生意,敢问路在何方?
  20. 一个老程序员的一些职场经验分享

热门文章

  1. java程序员待遇怎么样_上海海文告诉你Java程序员工资待遇到底如何
  2. windows系统禁止屏幕旋转快捷键
  3. vue网易严选购物商城项目
  4. 零基础学习云计算需要准备什么?
  5. 物联网单位换算:光的强度与辐射转换
  6. 计算机毕业设计Python毕业论文总结基于Python实现的仓库库存管理系统[包运行成功]
  7. 自己制作一个计时器、倒计时器
  8. 使用uniapp微信公众号和小程序踩坑全过程
  9. 关于MATLAB调用第三方程序
  10. java设计斗地主游戏引言_斗地主游戏设计 毕业论文.doc