Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args) throws Exception {
- Scanner in = new Scanner(System.in);
- PrintStream out = new PrintStream(System.out);
- int n = in.nextInt(), s = in.nextInt() - 1, f = in.nextInt() - 1, h = 0, t = 0, now;
- int[][] g = new int[n][n];
- int[] q = new int[n - 1], distance = new int[n];
- boolean[] used = new boolean[n];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j< n; j++) {
- g[i][j] = in.nextInt();
- }
- }
- q[t++ % q.length] = s;
- used[s] = true;
- while(h != t) {
- now = q[h++ % q.length];
- for(int i = 0; i < n; i++) {
- if(g[now][i] == 1 && !used[i]) {
- q[t++ % q.length] = i;
- used[i] = true;
- distance[i] = distance[now] + 1;
- }
- }
- }
- out.println(distance[f]);
- out.flush();
- }
- }
Add Comment
Please, Sign In to add comment