유용한 정보
DFS, BFS code
cosmohoo
2020. 7. 9. 12:37
반응형
=>BFS 와 DFS code를 사용하기 위해 미리 정리해두었습니다.
<code>
//int dist[100001]={0};
//bool check[100001];//갔다온지 확인하는 행렬
bool arr[MAX][MAX]; //인접행렬
vector<int> list[MAX]; //인접리스트
vector<pair<int,int>>edges; //간선리스트
void dfs(int x, int n)
{
check[x]=true;
for(int i=1; i<=n; i++)
{
if(arr[x][i] == true && check[i] ==false)//x, i가 연결되어있고 i를 가보지 않았던 경우
{
dfs(i, n);
}
}
}
void bfs(int x, int N)
{
queue<int> q;
check[x] = true;
q.push(x);
while (!q.empty())
{
int x = q.front();
q.pop();
for(int i=1; i<=N; i++)
{
if(arr[x][i] == true && check[i] == false)
{
check[i] = true;
q.push(i);
}
}
}
cout <<'\n';
}
반응형