Codeforces 475 B Strongly Connected City【DFS】

题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口

 

四个方向搜,搜完之后,判断每个点能够访问的点的数目是否相同,相同的话则说明可以到达任意点

 1 #include<iostream>  

 2 #include<cstdio>  

 3 #include<cstring> 

 4 #include <cmath> 

 5 #include<stack>

 6 #include<vector>

 7 #include<map> 

 8 #include<set>

 9 #include<queue> 

10 #include<algorithm>  

11 #define mod=1e9+7;

12 using namespace std;

13 

14 typedef long long LL;

15 const int INF = 0x7fffffff;

16 const int maxn=105;

17 char s1[maxn],s2[maxn];

18 int cnt[maxn][maxn],vis[maxn][maxn];

19 int n,m;

20 

21 void dfs(int x,int y){

22     if(x<1||x>n||y<1||y>m) return;

23     if(vis[x][y]) return;

24     vis[x][y]=1;

25     cnt[x][y]++;

26     if(s1[x]=='>') dfs(x,y+1);

27     if(s1[x]=='<') dfs(x,y-1);

28     if(s2[y]=='v') dfs(x+1,y);

29     if(s2[y]=='^') dfs(x-1,y);

30 }

31 

32 int main(){

33     int i,j;

34     scanf("%d %d",&n,&m);

35     cin>>(s1+1);

36     cin>>(s2+1);

37     

38     

39     memset(cnt,0,sizeof(cnt));

40     

41     for(i=1;i<=n;i++){

42         for(j=1;j<=m;j++){

43                 memset(vis,0,sizeof(vis));

44                 dfs(i,j);

45         }    

46     }

47     

48     //for(i=1;i<=n;i++){

49     //    for(j=1;j<=m;j++)

50     //    printf("cnt[%d][%d]=%d\n",i,j,cnt[i][j]);

51     //}

52     

53     for(i=1;i<=n;i++){

54         for(j=1;j<=m;j++){

55             if(cnt[i][j]!=cnt[1][1]){

56                 printf("NO\n");

57                 return 0;

58             }

59         }        

60     }

61     

62     printf("YES\n");

63     return 0;    

64 }
View Code

 

 

 

 

 

 

 

 

后来搜题解发现还有用强连通分量做的,还有用floyd做的= =

你可能感兴趣的:(codeforces)