-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diracted acyclic graph judgement.c
107 lines (88 loc) · 2.16 KB
/
Diracted acyclic graph judgement.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<stdio.h>
int main(){
int s,i,j,k;
int set,interger,num,end;
int mat[100][100];
int row_stack[100],col_stack[100],mark[100][100];
int ring,begin,p;
scanf("%d",&set);
for(s=1;s<=set;s++){
scanf("%d",&interger);
for(i=0;i<interger;i++){
for(j=0;j<interger;j++){
scanf("%d",&num);
mat[i][j]=num;
}
}
begin=-1;
for(i=0;i<interger;i++){
for(j=0;j<interger;j++){
if(mat[i][j]==1){
begin=i;
break;
}
}
if(begin!=-1)
break;
}
ring=0;
p=-1;
for(i=0;i<100;i++){ // initialize mark
for(j=0;j<100;j++){
mark[i][j]=0;
}
}
i=begin; // begin at beginning row
while(ring==0){
for(j=0;j<interger;j++){
if(mat[i][j]==1){
for(k=0;k<p;k++){
if(row_stack[k]==i && col_stack[k]==j){ //走到走過的地方
ring=1;
break;
}
}
if(ring==1){
break;
}
p++;
row_stack[p]=i;
col_stack[p]=j;
mark[i][j]=1; //走過變1
i=j; //從指定排開始找
j=-1; //歸零
}
else if(mat[i][j]==0){
if(j==interger-1){
if(p==-1){
ring=-1;
break;
}
i=row_stack[p];
j=col_stack[p];
p--;
if(j==interger-1&&p!=0){
i=row_stack[p];
j=col_stack[p];
p--;
}
else if(j==interger-1&&p==0){
ring=-1;
break;
}
}
}
}
}
if(ring==1&&s==set)
printf("Yes");
else if(ring==-1&&s==set)
printf("No");
else if(ring==1&&s!=set)
printf("Yes\n");
else
printf("No\n");
scanf("%d",&end);
}
return 0;
}