1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include<iostream> #include<cstring> #include<queue> #define endl "\n" #define MAX 100 using namespace std; int N; int MAP[MAX][MAX]; int Answer[MAX][MAX]; bool BFS_Visit[MAX]; bool DFS_Visit[MAX][MAX]; void Input() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> MAP[i][j]; } } } void BFS(int a) { queue<int> Q; Q.push(a); while (Q.empty() == 0) { int P = Q.front(); Q.pop(); for (int i = 0; i < N; i++) { if (MAP[P][i] == 1 && BFS_Visit[i] == false) { Q.push(i); BFS_Visit[i] = true; Answer[a][i] = 1; } } } } void DFS(int Point, int x, int y) { DFS_Visit[Point][y] = true; Answer[Point][y] = 1; for (int i = 0; i < N; i++) { if (DFS_Visit[Point][i] == false && MAP[y][i] == 1) { DFS(Point, y, i); } } } void Solution() { //########################################################################## // DFS Ver. for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (DFS_Visit[i][j] == false && MAP[i][j] == 1) { DFS(i, i, j); } } } //########################################################################## //for (int i = 0; i < N; i++) //{ // BFS(i); // memset(BFS_Visit, false, sizeof(BFS_Visit)); //} for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << Answer[i][j] << " "; } cout << endl; } } void Solve() { Input(); Solution(); } int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen("Input.txt", "r", stdin); Solve(); return 0; } | cs |
'[ BOJ Code ] > # BOJ -' 카테고리의 다른 글
[ 백준 7576 ] 토마토 (C++) (2) | 2018.11.28 |
---|---|
[ 백준 2667 ] 단지번호 붙이기 (C++) (0) | 2018.11.28 |
[ 백준 1697 ] 숨바꼭질 (C++) (0) | 2018.11.28 |
[ 백준 1260 ] DFS와 BFS (C++) (0) | 2018.11.28 |
[ 백준 1012 ] 유기농배추 (C++) (2) | 2018.11.28 |