Cod sursa(job #548442)

Utilizator Ael-ws00JF Basescu Ael-ws00 Data 7 martie 2011 14:25:03
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include<fstream.h>
#include<conio.h>

ifstream in("graf.in");

int a[20][20],s[20],viz[20],urm[20],sol[20],n,j;

void citire(){
int i,j;
in>>n;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
in>>a[i][j];
}

void df(int start){
int k=1,x,y,up;
s[k]=start;
viz[start]=1;
while(k>0){x=s[k];
					 up=0;
					 while(!up&&urm[x]<n){
								 urm[x]++;
								 if(!viz[urm[x]]&&a[urm[x]][x]==1){y=urm[x];
																									 up=1;
																									 }
								 }
					 if(up==1){k++;
										 s[k]=y;
										 viz[y]=1;
										 sol[j++]=y;
										 }
					 else k--;
					 }
}

void main(){
clrscr();
int start;
citire();
cout<<"start=";cin>>start;
sol[j++]=start;
df(start);
for(j=0;j<n;j++)cout<<sol[j]<<" ";
getch();
}