运用了与运算:两者都有才1,一个没有即为0;
特别容易错的地方:想用与运算什么的,只要是涉及了二进制的,都要加括号,二进制运算的优先级很低,特别低;只要涉及了二进制,都要加括号,二进制运算的优先级很低,特别低;只要是涉及了二进制的,都要加括号,二进制运算的优先级很低,特别低;
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 52;
int A[maxn][maxn], color[maxn][maxn];
int maxroomarea = 0, roomnum = 0, roomarea;
int m, n;
void dfs(int i, int j)
{
if(i > m || i < 1|| j > n||j < 1) return;
if(color[i][j]) return;
color[i][j] = roomnum;
roomarea++;
if(!(A[i][j]&1) ) dfs(i,j-1);
if(!(A[i][j]&2)) dfs(i-1,j);
if(!(A[i][j]&4)) dfs(i,j+1);
if(!(A[i][j]&8)) dfs(i+1,j);
}
int main()
{
scanf("%d%d",&m,&n);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
scanf("%d",&A[i][j]);
memset(color,0,sizeof(color));
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
{
if(!color[i][j]){
roomarea = 0;
roomnum++;
dfs(i,j);
maxroomarea = max(roomarea,maxroomarea);
}
}
printf("%d\n%d\n",roomnum,maxroomarea);
return 0;
}