&运算 按位与 3&1 = 1 011&001 = 001 = 1
此题中若maze[x][y] & 1 == 0 则 maze[x][y] 有西墙
同理可推得其他
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF = 99999999;
const int MOD = 1e9;
int maze[105][105];
bool visited[105][105];
int num = 0;
int maxnum = 0;
int n,m;
void dfs(int x,int y){
if(x < 0 || x >= n || y < 0 || y >= m) return;
if(visited[x][y]) return;
visited[x][y] = 1;
num++;
if((maze[x][y]&1) == 0){
dfs(x,y-1);
}
if((maze[x][y]&2) == 0){
dfs(x-1,y);
}
if((maze[x][y]&4) == 0){
dfs(x,y+1);
}
if((maze[x][y]&8) == 0){
dfs(x+1,y);
}
}
int main()
{
ios::sync_with_stdio(0);
cin >> n >> m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin >> maze[i][j];
}
}
int k = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(!visited[i][j]){
k++;
num = 0;
dfs(i,j);
maxnum = max(maxnum,num);
}
}
}
cout << k << endl << maxnum << endl;
return 0;
}