Guest User

Untitled

a guest
Jan 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.02 KB | None | 0 0
  1. import java.io.PrintStream;
  2. import java.util.Scanner;
  3.  
  4. public class Main{
  5.    public static void main(String[] args) throws Exception {
  6.        Scanner in = new Scanner(System.in);
  7.        PrintStream out = new PrintStream(System.out);
  8.        int n = in.nextInt(), s = in.nextInt() - 1, f = in.nextInt() - 1, h = 0, t = 0, now;
  9.        int[][] g = new int[n][n];
  10.        int[] q = new int[n - 1], distance = new int[n];
  11.        boolean[] used = new boolean[n];        
  12.        
  13.        for(int i = 0; i < n; i++) {
  14.            for(int j = 0; j< n; j++) {
  15.                g[i][j] = in.nextInt();
  16.            }
  17.        }
  18.        
  19.        q[t++ % q.length] = s;
  20.        used[s] = true;
  21.        while(h !=  t) {
  22.            now = q[h++ % q.length];
  23.            for(int i = 0; i < n; i++) {
  24.                if(g[now][i] == 1 && !used[i]) {
  25.                    q[t++ % q.length] = i;
  26.                    used[i] = true;
  27.                    distance[i] = distance[now] + 1;
  28.                }
  29.            }
  30.        }
  31.        out.println(distance[f]);
  32.        out.flush();
  33.    }
  34. }
Add Comment
Please, Sign In to add comment