Advertisement
Guest User

Untitled

a guest
Jan 19th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.*;
  2. import java.math.*;
  3.  
  4. public class GogoXMarisaKirisima
  5. {
  6.     int n;
  7.     int answer = 0;
  8.     boolean[][] matrix;
  9.     boolean[][] accessible;
  10.     boolean visited[];
  11.    
  12.     void dfs(int from)
  13.     {
  14.         visited[from] = true;
  15.         for (int i = 0; i < n; i++)
  16.         {
  17.             if (matrix[from][i])
  18.             {
  19.                 if (visited[i] || i == n - 1)
  20.                 {
  21.                     answer++;
  22.                 }
  23.                 if (!visited[i] && accessible[i][n - 1])
  24.                 {
  25.                     dfs(i);
  26.                 }
  27.             }
  28.         }
  29.     }
  30.    
  31.     public int solve(String[] choices)
  32.     {
  33.         n = choices.length;
  34.         matrix = new boolean[n][n];
  35.        
  36.         for (int i = 0; i < n; i++)
  37.         {
  38.             for (int j = 0; j < n; j++)
  39.             {
  40.                 matrix[i][j] = choices[i].charAt(j) == 'Y' ? true : false;
  41.             }
  42.         }
  43.        
  44.         accessible = new boolean[n][n];
  45.         for (int i = 0; i < n; i++)
  46.         {
  47.             for (int j = 0; j < n; j++)
  48.             {
  49.                 accessible[i][j] = matrix[i][j];
  50.             }
  51.         }
  52.        
  53.         for (int k = 0; k < n; k++)
  54.         {
  55.             for (int i = 0; i < n; i++)
  56.             {
  57.                 for (int j = 0; j < n; j++)
  58.                 {
  59.                     if (accessible[i][k] && accessible[k][j])
  60.                     {
  61.                         accessible[i][j] = true;
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.        
  67.         if (!accessible[0][n - 1])
  68.             return 0;
  69.  
  70.         visited = new boolean[n];
  71.        
  72.         dfs(0);
  73.        
  74.         return answer;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement