百练2815城堡问题dfs

就是那个位运算,,,,,我又想多了emmmmm'

其实不同的位对应不同的递归而已

那个& 别弄错逻辑

如果有,就不能走!!!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int n,m;
 8 const int maxn = 50+5;
 9 int mat[maxn][maxn];
10 int color[maxn][maxn];
11 int mj,maxmj=0;
12 
13 void dfs(int x,int y){
14     if(color[x][y]!=0)
15         return ;
16     color[x][y]=1;
17     mj++;
18     if((mat[x][y]&1)==0) dfs(x,y-1);
19     if((mat[x][y]&2)==0) dfs(x-1,y);
20     if((mat[x][y]&4)==0) dfs(x,y+1);
21     if((mat[x][y]&8)==0) dfs(x+1,y);
22     //别一看见位运算就不知道它在干嘛了
23 }
24 int main(){
25     int count = 0;
26     memset(color,0,sizeof(color));
27     scanf("%d%d",&n,&m);
28     for(int i = 0;i<n;i++){
29         for(int j = 0;j<m;j++){
30             scanf("%d",&mat[i][j]);
31         }
32     }
33     for(int i = 0;i<n;i++){
34         for(int j = 0 ;j<m;j++){
35             if(color[i][j]==0)
36             {
37                 mj = 0;
38                 dfs(i,j);
39                 count++;
40                 if(mj>maxmj) maxmj=mj;
41             }    }
42     }
43     cout<<count<<endl;
44     cout<<maxmj<<endl;
45     return 0;
46 }

猜你喜欢

转载自www.cnblogs.com/zhmlzhml/p/13398016.html