cjy_lsmdsb @ 2025-01-10 20:30:52
#include <iostream>
#include <vector>
using namespace std;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
void dfs(vector<vector<int>>& image, int x, int y, int n) {
if(x < 0 || x >= n || y < 0 || y >= n || image[x][y] == 0) return;
image[x][y] = 0;
for(int i = 0; i < 4; ++i) {
int newX = x + dx[i];
int newY = y + dy[i];
dfs(image, newX, newY, n);
}
}
int main() {
int n;
cin >> n;
vector<vector<int>> image(n, vector<int>(n));
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
cin >> image[i][j];
}
}
int count = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(image[i][j] == 1) {
dfs(image, i, j, n);
count++;
}
}
}
cout << count << endl;
return 0;
}
by cjy_lsmdsb @ 2025-01-10 20:33:54
求代码,大神们
by CJR_Rain @ 2025-01-10 20:58:50
@cjy_lsmdsb这题我会,给我10分钟
by CJR_Rain @ 2025-01-10 21:04:17
@cjy_lsmdsb这题是八个方向都要搜……我刚写完也错了
by shawn0618 @ 2025-01-10 21:19:15
@cjy_lsmdsb这数据范围是让你写dfs的吗?!