import java.util.Stack;
public class DFSMy {
public static void main(String[] args)
{
int graph[][] = {{0, 1, 1, 0,0},
{1, 0, 0, 0,1},
{1, 0, 0, 1,0},
{0, 0, 1, 0,0},
{0, 1, 0, 0,0},};
boolean visited[] = new boolean[10];
Stack<Integer> stack = new Stack<Integer>();
int n = 5;
for(int i=0;i<10;i++)
visited[i]=false;
//for(int i=0;i<n;i++)
//{
//if(!visited[i])
//{
dfs(stack,graph,visited,0,n);
//}
//}
}
public static void dfs(Stack<Integer> stack,int array[][],boolean[] visited,int i,int n)
{
visited[i] = true;
stack.push(i);
int k;
System.out.print(i+1+" ");
if(!stack.isEmpty())
return;
k = stack.pop();
for(int j=0;j<n;j++)
{
if(array[k][j]!=0&&!visited[j])
{
//stack.push(j);
//visited[j] = true;
//System.out.print(j+1+" ");
//break;
dfs(stack,array,visited,j,n);//对未访问的邻接点递归调用dfs
}
}
}
}