1 条题解
-
0Infinity_ LV 8 @ 2024-08-22 16:59:25
由于N不分割0构成的区域,而B分割
所以先搜索有几个由0构成的区域,+1再/2就是答案了
(有没有大佬知道怎么算N的数量)#include<bits/stdc++.h> #define N 1010 using namespace std; char b[N][N]; bool vis[N][N]; int p[8][3] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}}, m, n; struct node{ int x, y; }q[N]; void bfs(int x, int y){ int head = 1, tail = 1; q[tail].x = x; q[tail].y = y; tail++; while(head < tail){ vis[q[head].x][q[head].y] = 1; for(int i = 0; i < 8; i++){ int nx = q[head].x + p[i][0], ny = q[head].y + p[i][1]; if(nx >= 0 && nx <= m+1 && ny >= 0 && ny <= n+1 && b[nx][ny] == '0' && !vis[nx][ny]){ q[tail].x = nx; q[tail].y = ny; //入队 tail++; vis[nx][ny] = 1; } } head++; //队头出队 } } int main(){ ios::sync_with_stdio(false); int cnt = 0; cin >> m >> n; memset(vis, 0, sizeof(vis)); memset(b, 0, sizeof(b)); for(int i = 1; i <= m; i++){ for(int j = 1; j <= n; j++)cin >> b[i][j]; } for(int i = 1; i <= m; i++){ for(int j = 1; j <= n; j++){ if(b[i][j] == '0' && !vis[i][j]){ cnt++; bfs(i, j); } } } cout << (cnt-1)/2; return 0; }
吐槽一下,这题太坑了,说好的m,n < 30,但我开到50还是炸了
- 1
信息
- ID
- 1210
- 难度
- 9
- 分类
- (无)
- 标签
- 递交数
- 2
- 已通过
- 1
- 通过率
- 50%
- 上传者