본문 바로가기
프로그래밍/문제풀이

[dfs] 백준 11403 경로 찾기

by 하용권 2019. 3. 27.

https://www.acmicpc.net/problem/11403

 

11403번: 경로 찾기

가중치 없는 방향 그래프 G가 주어졌을 때, 모든 정점 (i, j)에 대해서, i에서 j로 가는 경로가 있는지 없는지 구하는 프로그램을 작성하시오.

www.acmicpc.net

#include <iostream>
#include <vector>
using namespace std;
vector<int> v[100];
int ret[100][100] = {0};
int n;

void dfs(int start, int y){
	
	for(int i = 0; i < v[start].size(); i++){
		int tmp = v[start][i];
		if(ret[y][tmp]) continue;
		ret[y][tmp] = 1;
		dfs(tmp,y);
	}
}

int main(){
	int tmp;
	cin >> n;
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			cin >> tmp;
			if(tmp == 1)
				v[i].push_back(j);
		}	
	}
	
	
	for(int i = 0; i < n; i++){
		dfs(i,i);
	}
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++)
			cout << ret[i][j] << ' ';
		cout << endl;
	}
}	
반응형

'프로그래밍 > 문제풀이' 카테고리의 다른 글

[bfs] 백준 1963 소수 경로  (0) 2019.04.01
[bfs] 백준 2589 보물섬  (0) 2019.04.01
[bfs] 백준 1389 케빈 베이컨의 6단계 법칙  (0) 2019.03.27
[dfs] 백준 2644 촌수계산  (0) 2019.03.27
[dfs] 백준 9903 로또  (0) 2019.03.27