Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.5 kB
1
Indexable
Never
protected List<E> visitDFPath2(Vertex<E> v, Vertex<E> e, Set<Vertex<E>> visited, Set<E> blockedCells, int trollsFought) {
        if (v.equals(e)) {
            List<E> path = new LinkedList<>();
            path.add(e.getValue());
            return path;
        } else {
            for (Vertex<E> neighbor : v.getNeighbors()) {
                if (!visited.contains(neighbor)) {
                    if (blockedCells.contains(neighbor.getValue()) && trollsFought < 1){
                        visited.add(neighbor);
                        List<E> path = visitDFPath2(neighbor, e, visited, blockedCells, trollsFought + 1);
                        if (path == null){
                            visited.remove(neighbor);
                        }
                        else{
                            if (path != null) {
                                path.add(0, v.getValue());
                                return path;
                            }
                        }
                    }
                    else if (!blockedCells.contains(neighbor.getValue())) {
                        visited.add(neighbor);
                        List<E> path = visitDFPath2(neighbor, e, visited, blockedCells, trollsFought);
                        if (path != null) {
                            path.add(0, v.getValue());
                            return path;
                        }
                    }
                    
                }
            }
            return null;
        }
    }