-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMi.java
More file actions
46 lines (44 loc) · 1.18 KB
/
Mi.java
File metadata and controls
46 lines (44 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
public class Mi {
static private final int POINT_NUM = 9;
static private int[][] e={
{0,0,0,0,0,0,0,0,1},
{0,0,0,1,1,0,0,0,0},
{1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0},
{0,0,0,0,0,1,0,0,0}};
static ArrayList<Integer> trace=new ArrayList<Integer>();
static int j;
static boolean hasCycle=false;
public static void main(String[] args) {
findCycle(0);
if(!hasCycle)
System.out.println("No Cycle.");
}
static void findCycle(int v)
{
if((j=trace.indexOf(v))!=-1)
{
hasCycle=true;
System.out.print("Cycle:");
while(j<trace.size())
{
System.out.print(trace.get(j)+" ");
j++;
}
System.out.print("\n");
return;
}
trace.add(v);
for(int i=0;i<POINT_NUM;i++)
{
if(e[v][i]==1)
findCycle(i);
}
trace.remove(trace.size()-1);
}
}