回溯算法应用-有向回路

笔试原题:花了三个小时 Java数据结构不熟练啊

从家出发,经历一个完全上坡和完全下坡,回到家,求最短路程

Now you have a dictionary of places in Beijing. It's in the form of {location: elevation}. And an array of distances you find on Baidu Map connecting each places. Please find the length of the shortest route on which you can run completely uphill then completely downhill. Assume you live in "Huilongguan".

elevations = {"Huilongguan": 5, "Chaoyang Park": 25, "National Stadium": 15, "Olympic Park": 20, "Tsinghua University": 10}

paths = {

    ("Huilongguan", "Chaoyang Park"): 10,

    ("Huilongguan", "National Stadium"): 8,

    ("Huilongguan", "Olympic Park"): 15,

    ("Chaoyang Park", "Olympic Park"): 12,

    ("National Stadium", "Tsinghua University"): 10,

    ("Olympic Park", "Tsinghua University"): 5,

    ("Olympic Park", "Huilongguan"): 17,

    ("Tsinghua University", "Huilongguan"): 10

}

For this set of data, the shortest valid path would be "Huilongguan" -> "National Stadium" -> "Tsinghua University" -> "Huilongguan", with a distance of 28.

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

public class dist {

static String home;

 

public void backtrack(List<List<String>> res,List<String> tempList, String root,

HashMap<String, ArrayList<String>> output,int sum, List<Integer> sumArray,HashMap<List<String>,Integer > paths) {

if(!output.containsKey(root)) {//没有出口

return;

}

if(tempList.size()>2&&tempList.get(tempList.size()-1).equals(home)) {

res.add(new ArrayList<>(tempList));

sumArray.add(sum);

return;

}else {

List<String> x = output.get(root);

for(int i=0;i<x.size();i++) {

tempList.add(x.get(i));

ArrayList<String> path = new ArrayList<String>();

path.add(root);

path.add(x.get(i));

// System.out.println(paths.get(path));

sum +=paths.get(path);

backtrack(res,tempList,x.get(i),output,sum,sumArray,paths);

//System.out.println("tempList的内容:"+tempList+"-------"+"sum:"+sum);

tempList.remove(tempList.size()-1);

sum -=paths.get(path);

}

}

}

 

public static void main(String[] args) {

dist.home = "H";

HashMap<List<String>,Integer > paths = new HashMap<List<String>,Integer >();

String[] s1 = {"H","C"};List<String> ss1 = Arrays.asList(s1);

String[] s2 = {"H","N"};List<String> ss2 = Arrays.asList(s2);

String[] s3 = {"H","O"};List<String> ss3 = Arrays.asList(s3);

String[] s4 = {"C","O"};List<String> ss4 = Arrays.asList(s4);

String[] s5 = {"N","T"};List<String> ss5 = Arrays.asList(s5);

String[] s6 = {"O","T"};List<String> ss6 = Arrays.asList(s6);

String[] s7 = {"O","H"};List<String> ss7 = Arrays.asList(s7);

String[] s8 = {"T","H"};List<String> ss8 = Arrays.asList(s8);

paths.put(ss1,10);

paths.put(ss2,8);

paths.put(ss3,15);

paths.put(ss4,12);

paths.put(ss5,10);

paths.put(ss6,5);

paths.put(ss7,17);

paths.put(ss8,10);

HashMap<String, ArrayList<String>> output = new HashMap<String, ArrayList<String>>();

for (List<String> entry: paths.keySet()) {

String x = entry.get(0);

String y = entry.get(1);

if(output.containsKey(x)) {

output.get(x).add(y);

}else {

ArrayList<String> str = new ArrayList<String>();

str.add(y);

output.put(x,str);

}

}

output.entrySet().stream().forEachOrdered((entry) -> {

            Object currentKey = entry.getKey();

            Object currentValue = entry.getValue();

//            System.out.println(currentKey + "=" + currentValue);

        });

List<List<String>> res = new ArrayList<>();

dist dist = new dist();

ArrayList<String> tmp = new ArrayList<String>();

tmp.add(home);

List<Integer> sumarray = new ArrayList<Integer>();

dist.backtrack(res,tmp,home,output,0,sumarray,paths);

for(int i=0;i<res.size();i++) {

System.out.println("PATH:"+res.get(i)+" DIST: "+sumarray.get(i));

}

HashMap<String, Integer> elevation = new HashMap<>();

elevation.put("H", 5);

elevation.put("C", 25);

elevation.put("N", 15);

elevation.put("O", 20);

elevation.put("T", 10);

for(int i=0;i<res.size();i++) {

List<String> a = res.get(i);

int up = 0;

int down = 0;

for(int j=0;j<a.size()-1;j++) {

if(up==0) {

if(elevation.get(a.get(j))>elevation.get(a.get(j+1))) {//先下

System.out.println("down fault ");

res.remove(i);

break;

}else {//向上

up = 1;

}

}else if(up==1) {

if(elevation.get(a.get(j))>elevation.get(a.get(j+1))) {//向下

down = 1;

}else {//向上

if(down==1) {

System.out.println("up fault ");

res.remove(i);

break;

}

}

}

}

}

System.out.println("---------after up and down check----------");

int min = Integer.MAX_VALUE;

int item = -1;

for(int i=0;i<res.size();i++) {

System.out.println("PATH:"+res.get(i)+" DIST: "+sumarray.get(i));

if(sumarray.get(i)<min) {

min = sumarray.get(i);

item = i;

}

}

System.out.println("---------the shortest path----------");

System.out.println("PATH:"+res.get(item)+" DIST: "+sumarray.get(item));

}

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值