Skip to content

Instantly share code, notes, and snippets.

@bitcpf
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcpf/629c20bb88618ac70df0 to your computer and use it in GitHub Desktop.
Save bitcpf/629c20bb88618ac70df0 to your computer and use it in GitHub Desktop.
package cc150_4_2;
import java.util.*;
public class CheckRoute {
public static boolean Route(int[][] graph, int head, int endp){
if(graph.length < 2) return false;
List<Integer> visited = new ArrayList<Integer>();
visited.add(head);
int c_p = 0;
int g_p = visited.get(c_p)-1;
int flag = c_p;
while(!visited.contains(endp) && visited.size() < graph.length){
for(int i = 0;i < graph.length; i++){
if(graph[g_p][i] == 1 && !visited.contains(i+1)){
visited.add(i+1);
System.out.println("inloop visited: "+g_p+": "+(i+1));
}
}
System.out.println("visited node: "+visited.toString());
System.out.println("C_p value: "+c_p);
if(c_p < visited.size()-1){
c_p ++;
g_p = visited.get(c_p)-1;
}
if(c_p == flag){
break;
}
else
{flag = c_p;}
}
if(visited.contains(endp)){
return true;
}
else return false;
}
public static void main(String[] args){
int[][] test = new int[][]{
{ 0, 1, 0, 1, },
{ 1, 0, 0, 1, },
{ 0, 0, 0, 0, },
{ 1, 1, 0, 0, },
};
/*
* a--b--c
* \ |
* \|
* d
*/
System.out.println(Route(test,1,3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment