莫回无
一个简单的 BFS 在网格上应该可以解决问题:#include <bits/stdc++.h>using namespace std;int grid[5][5];int mark[5][5];vector<vector<pair<int, int> > > groups;void bfs(int row, int col){ queue<pair<int, int>> q; q.push(make_pair(row, col)); vector<pair<int, int>> group; while(q.size() > 0){ pair<int, int> p = q.front(); q.pop(); if(mark[p.first][p.second]) continue; mark[p.first][p.second] = 1; group.push_back(p); for(int i = -1; i < 2; i++){ for(int j = -1; j < 2; j++){ if(0 > p.first + i) continue; if(p.first + i >= 5) continue; if(0 > p.second + j) continue; if(p.second + j >= 5) continue; if(mark[p.first + i][p.second + j]) continue; if(!grid[p.first + i][p.second + j]) continue; q.push(make_pair(p.first + i, p.second + j)); } } } groups.push_back(group);}int main(){ groups.clear(); memset(grid, 0, sizeof(grid)); memset(mark, 0, sizeof(mark)); grid[0][0] = 1; grid[0][1] = 1; grid[2][2] = 1; grid[2][3] = 1; grid[3][3] = 1; grid[3][4] = 1; for(int i = 0; i < 5; i++){ for(int j = 0; j < 5; j++){ if(mark[i][j]) continue; if(grid[i][j]) bfs(i, j); } } for(int i = 0; i < groups.size(); i++){ cout<<"group "<<(i + 1)<<": "; for(int j = 0; j < groups[i].size(); j++){ cout<<"("<<groups[i][j].first<<", "<<groups[i][j].second<<") "; } cout<<endl; } return 0;}添加了一个具有 5x5 网格的工作示例,其中 1 表示 。x
www说
您可以访问所有单元格,并对连接的岛屿使用计数器。function check(array) { function test(array, i, j, value) { if (array[i] && array[i][j] === -1) { array[i][j] = value; test(array, i - 1, j - 1, value); test(array, i - 1, j, value); test(array, i - 1, j + 1, value); test(array, i + 1, j - 1, value); test(array, i + 1, j, value); test(array, i + 1, j + 1, value); test(array, i, j - 1, value); test(array, i, j + 1, value); return true; } } var value = 1; array.forEach(a => a.forEach((b, i, bb) => bb[i] = -b)); array.forEach((a, i, aa) => a.forEach((b, j) => test(aa, i, j, value) && value++)); document.getElementById('out').innerHTML += array.map(a => a.join(' ')).join('\n');}check([' xx xxxx xx x ', ' xxx xxxx xx xx ', ' x x x x xx ', ' xxx xx xx ', ' x x x xx xx ', 'xxx xxx xx x', 'xx xxx xx xx'].map(s => [...s].map(v => +(v !== ' '))));<pre id="out"></pre>