Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.math.*;
- public class GogoXMarisaKirisima
- {
- int n;
- int answer = 0;
- boolean[][] matrix;
- boolean[][] accessible;
- boolean visited[];
- void dfs(int from)
- {
- visited[from] = true;
- for (int i = 0; i < n; i++)
- {
- if (matrix[from][i])
- {
- if (visited[i] || i == n - 1)
- {
- answer++;
- }
- if (!visited[i] && accessible[i][n - 1])
- {
- dfs(i);
- }
- }
- }
- }
- public int solve(String[] choices)
- {
- n = choices.length;
- matrix = new boolean[n][n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- matrix[i][j] = choices[i].charAt(j) == 'Y' ? true : false;
- }
- }
- accessible = new boolean[n][n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- accessible[i][j] = matrix[i][j];
- }
- }
- for (int k = 0; k < n; k++)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (accessible[i][k] && accessible[k][j])
- {
- accessible[i][j] = true;
- }
- }
- }
- }
- if (!accessible[0][n - 1])
- return 0;
- visited = new boolean[n];
- dfs(0);
- return answer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement