判断三元环

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#define NN 2001
char a[NN][NN];
int mark[NN], pre[NN];
int start,end;
int n;

/*bool dfs(int prev, int cur) {
    mark[cur] = 1;
    for (int next=0;next<n;++next)
        if (a[cur][next]-'0') {
            if (a[next][prev]-'0')
                return true;
            else if (!mark[next] && dfs(cur,next))
                return true;
        }
    return false;
}*/
bool dfs(int now){
    mark[now]=1;
    for(int i=0;i<n;i++){
        if (a[now][i]-'0') {
            if(mark[i]==1)return 1;
            if(mark[i]==0 && dfs(i))return 1;
        }
    }
    mark[now]=-1;
    return 0;
}

int main () {
    int t,T,i,j;
    scanf("%d",&T);
    for(t=1;t<=T;t++){
        printf("Case #%d: ",t);
        bool flag=1;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%s",a[i]);
        memset(mark,0,sizeof(mark));
        for (int i=0;i<n;++i)
            if (!mark[i] && dfs(i)){
                 printf("Yes\n");
                 flag=0;
                 break;
            }
        if(flag)
        printf("No\n");
    }
    return 0;
}