-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcs1.py
64 lines (50 loc) · 2.04 KB
/
tcs1.py
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
# Consider a set of web pages, numbered \
# from 1 to N. Each web page has links to one or more web pages. Clicking on a link in a page, takes one
# to the other web page. You are provided numbers of two web pages viz, starting web page and
# end web page. Your task is to find the minimum number of clicks required to reach the end page from the start page.
# If end page cannot be reached from start page, print -1 as the output. For better understanding refer Examples sectio
# num=int(input("Number of Webpages: "))
# linked=int(input("Number of linked web pages"))
# print(f"Number of click's required {num*linked}")
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static int findMinClicks(int N, ArrayList[] pages, int start, int end) {
boolean[] visited = new boolean[N + 1];
Queue> queue = new LinkedList<>();
queue.add(new Pair<>(start, 0));
while (!queue.isEmpty()) {
Pair current = queue.poll();
int current_page = current.getKey();
int clicks = current.getValue();
visited[current_page] = true;
if (current_page == end) {
return clicks;
}
for (int linked_page : pages[current_page]) {
if (!visited[linked_page]) {
queue.add(new Pair<>(linked_page, clicks + 1));
}
}
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
ArrayList[] pages = new ArrayList[N + 1];
for (int i = 1; i <= N; i++) {
int numLinks = scanner.nextInt();
pages[i] = new ArrayList<>(numLinks);
for (int j = 0; j < numLinks; j++) {
pages[i].add(scanner.nextInt());
}
}
int start = scanner.nextInt();
int end = scanner.nextInt();
int result = findMinClicks(N, pages, start, end);
System.out.println(result);
}
}