poj 1164 dfs 位运算

好像是第一次用位运算。。。得习惯二进制。。

还有就是p=0的时候。。。

View Code
 1 // File Name: 1164.cpp
 2 // Author: Missa
 3 // Created Time: 2013/2/2 星期六 23:08:26
 4 
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<algorithm>
 9 #include<cmath>
10 #include<queue>
11 #include<stack>
12 #include<string>
13 #include<vector>
14 #include<cstdlib>
15 #include<map>
16 using namespace std;
17 
18 const int maxn = 55;
19 
20 int ma[maxn][maxn];
21 int ans,large,tmp;
22 int n,m;
23 bool ok(int x,int y)
24 {
25     if(x<1 || x>n || y<1 || y>m)
26         return false;
27     return true;
28 }
29 void dfs(int x,int y)
30 {
31     int t=ma[x][y];
32     ma[x][y]=-1;
33     int ntx,nty;
34     tmp++;
35     for(int k=0;k<4;k++)
36     {
37         if(t & (1<<k) ) continue;
38         switch(k)
39         {
40             case 0:
41                 ntx=x,nty=y-1;
42                 break;
43             case 1:
44                 ntx=x-1,nty=y;
45                 break;
46             case 2:
47                 ntx=x,nty=y+1;
48                 break;
49             case 3:
50                 ntx=x+1,nty=y;
51                 break;
52         }
53         if(!ok(ntx,nty)) continue;
54         if(ma[ntx][nty] != -1)
55             dfs(ntx,nty);
56     }
57 }
58 int main()
59 {
60     while(~scanf("%d%d",&n,&m))
61     {
62         for(int i=1;i<=n;i++)
63             for(int j=1;j<=m;j++)
64                 scanf("%d",&ma[i][j]);
65         ans=0,large=0;
66         for(int i=1;i<=n;i++)
67             for(int j=1;j<=m;j++)
68             {
69                 if(ma[i][j]==-1) continue;
70                 ans++;
71                 tmp=0;
72                 dfs(i,j);
73                 if(tmp>large)
74                     large=tmp;
75             }
76         printf("%d\n%d\n",ans,large);
77     }
78     return 0;
79 }
原文地址:https://www.cnblogs.com/Missa/p/2890768.html