题目链接 acwing 1098.城堡问题
求大佬解答为啥
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
//int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
我换成下面那个就不对了
错误的代码:
// 用二进制表示的状态, 太强了,以后看到 1 2 4 8 要引起注意
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#define x first
#define y second
using namespace std;
typedef pair<int ,int> PII;
const int N = 60;
bool yin[N][N];
int g[N][N];
int n, m;
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
//int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
queue<PII> q;
int bfs(int a, int b)
{
q.push({a, b});
int area = 0;
yin[a][b] = true;
while(q.size())
{
auto t = q.front();
q.pop();
area ++;
for(int i = 0; i < 4; i ++)
{
int sx = t.x + dx[i], sy = t.y + dy[i];
if(sx >= n || sx < 0 || sy >= m || sy < 0 || yin[sx][sy]) continue;
if(g[t.x][t.y] >> i & 1) continue;
yin[sx][sy] = true;
q.push({sx, sy});
}
}
return area;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
cin >> g[i][j];
int cnt = 0, ares = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
{
if(!yin[i][j])
{
ares = max (ares, bfs(i, j));
yin[i][j] = true;
cnt ++;
}
}
cout << cnt << endl;
cout << ares << endl;
return 0;
}
不知道为啥错了
提问于17天前
10967