#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int MAX = 101;
int n;
int way[MAX][MAX];
int visited[MAX][MAX];
int bfs(int i,int j) {
int answer = 0;
int flag = 0;
if (i == j)
flag = 1;
queue<int> q;
q.push(i);
while (!q.empty()) {
int current = q.front();
if (current == j ) {
if (flag == 2 || flag == 0) {
answer = 1;
break;
}
else
flag++;
}
q.pop();
for (int k = 0; k < n; k++) {
if (way[current][k] == 1 && visited[current][k] == 0) {
q.push(k);
visited[current][k] = 1;
}
}
}
return answer;
}
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> way[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (way[i][j] == 0) {
memset(visited, 0, sizeof(visited));
cout << bfs(i, j) << " ";
}
else
cout << 1 << " ";
}
cout << endl;
}
return 0;
}
'백준 알고리즘 > BFS' 카테고리의 다른 글
백준 7569번 C++ (0) | 2019.08.10 |
---|---|
백준 2468번 C++ (0) | 2019.08.10 |
백준 1012번 C++ (0) | 2019.08.08 |
백준 7576번 C++ (0) | 2019.08.08 |
백준 2667번 C++ (0) | 2019.08.08 |