参考题目
https://blog.csdn.net/luoyeliufeng/article/details/103637935
代码
#include <iostream>
#include <stack>
using namespace std;
class Graph
{
int **matrix;
int vexNum;
bool *visited;
void DFS(int v);
public:
Graph(){
cin>>vexNum;
visited = new bool[vexNum];
matrix = new int*[vexNum];
for (int i = 0; i < vexNum; ++i) {
matrix[i] = new int[vexNum];
for (int j = 0; j < vexNum; ++j) {
cin>>matrix[i][j];
}
}
};
~Graph(){
delete []visited;
for (int i = 0; i < vexNum; ++i) {
delete []matrix[i];
}
delete []matrix;
}
void DFS();
};
void Graph::DFS(int v) {
stack<int> s;
if(!visited[v])
s.push(v);
while (!s.empty())
{
v = s.top();
if (!visited[v])
cou