Last active
August 29, 2015 14:03
-
-
Save bitcpf/629c20bb88618ac70df0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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