-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectedGraph.java
More file actions
97 lines (74 loc) · 2.5 KB
/
DirectedGraph.java
File metadata and controls
97 lines (74 loc) · 2.5 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//2008 - 6
import java.util.*;
public class DirectedGraph{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner inputS = new Scanner(System.in);
int n = input.nextInt();
String[][] matrix= new String[n][n];
for(int i = 0; i < n; i++){
String rawIn = inputS.nextLine();
String[] oneLine = rawIn.split(" ");
for(int x = 0; x < n; x++){
matrix[i][x] = oneLine[x];
}
}
int times = input.nextInt();
for(int i = 0; i < times; i++){
String rawIn = inputS.nextLine();
String[] rawLine = rawIn.split(" ");
int begin = Integer.parseInt(rawLine[0]);
int end = Integer.parseInt(rawLine[1]);
int distance = 0;
ArrayList<Integer> beginStates = new ArrayList<Integer>();
HashSet<Integer> inspectedStates = new HashSet<Integer>();
beginStates.add(begin);
boolean go = true;
if(begin == end){
System.out.println("distance from " + begin + " to " + end + " is " + distance);
continue;
}
ArrayList<Integer> newBeginStates = new ArrayList<Integer>();
//Do stuff
while(go){
distance++;
for(int x = 0; x < beginStates.size(); x++){
int tempState = beginStates.get(x);
for(int j = 0; j < n; j++){
if(matrix[tempState][j].equals("1")){
if(j == end){
//We got it
System.out.println("distance from " + begin + " to " + end + " is " + distance);
go = false;
beginStates.clear();
break;
} else {
//Not the end, but add it to newBeginStates
newBeginStates.add(j);
}
}
}
inspectedStates.add(tempState);
}
beginStates.clear();
for(int x = 0; x < newBeginStates.size(); x++){
if(!beginStates.contains(newBeginStates.get(x))){
beginStates.add(newBeginStates.get(x));
}
}
newBeginStates.clear();
boolean valid = false;
for(int x = 0; x < beginStates.size(); x++){
if(!inspectedStates.contains(beginStates.get(x))){
valid = true;
}
}
if(!valid && go){
go = false;
System.out.println("no path from " + begin + " to " + end);
break;
}
}
}
}
}