문제는 다음과 같습니다.
https://www.acmicpc.net/problem/11403
import java.util.Scanner;
public class Test11403 {
static int N;
static int[][] adMatrix;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
adMatrix = new int[N+1][N+1];
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
adMatrix[i][j] = sc.nextInt();
}
}
//알고리즘 풀이
for(int i=1; i<=N; i++) {
int[] visit = new int[N+1];
DFS(i, visit, false);
for(int j=1; j<=N; j++) {
System.out.print(visit[j]+" ");
}System.out.println();
}
}
static void DFS(int x, int[] visit, Boolean isFirst) {
if(isFirst) {
visit[x] = 1;
}
for(int i=1; i<=N; i++) {
if(adMatrix[x][i]==1 && visit[i]==0) {
DFS(i, visit, true);
}
}
}
}
import java.util.Scanner;
public class Test11403{
static int N;
static int[][] adMatrix;
static void DFS(int x, int[] visit, boolean isFirst) {
if(!isFirst) { //isFirst가 false일 경우
visit[x]=1;
}
for(int i=1; i<=N; i++) {
if(adMatrix[x][i]==1 && visit[i]==0) {
DFS(i, visit, false);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
adMatrix = new int[N+1][N+1];
//행렬 입력받기
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
adMatrix[i][j] = sc.nextInt();
}
}
//알고리즘 풀이
for(int i=1; i<=N; i++) {
int[] visit = new int[N+1];
DFS(i, visit, true);
for(int j=1; j<=N; j++) {
System.out.print(visit[j]+" ");
}System.out.println();
}
}
}
import java.util.Scanner;
public class Test11403{
static int N;
static int[][] adMatrix;
static int[][] result;
static void DFS(int x, int[] visit, boolean isFirst) {
if(!isFirst) { //isFirst가 false일 경우
visit[x]=1;
}
for(int i=1; i<=N; i++) {
if(adMatrix[x][i]==1 && visit[i]==0) {
DFS(i, visit, false);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
adMatrix = new int[N+1][N+1];
result = new int[N+1][N+1];
//행렬 입력받기
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
adMatrix[i][j] = sc.nextInt();
}
}
//알고리즘 풀이
for(int i=1; i<=N; i++) {
int[] visit = new int[N+1];
DFS(i, visit, true);
result[i] = visit;
}
//출력하기
for(int i=1; i<result.length; i++) {
for(int j=1; j<result.length; j++) {
System.out.print(result[i][j]+" ");
}
if(i != result.length-1) {
System.out.println();
}
}
}
}
결과입니다.