本文共 741 字,大约阅读时间需要 2 分钟。
题目:求岛屿的数目,所有相连的1构成一个岛屿。
思路:BFS
class Solution {public: int sum = 0; void BFS(vector>& grid,int x,int y){ int dir[4][2]={ {-1,0},{ 1,0},{ 0,-1},{ 0,1}}; struct Q{ int x; int y; }; Q q[2500]; q[0].x = x; q[0].y = y; grid[x][y] = '0'; int l=0,r=1; while(l = grid.size() || temp.y < 0 || temp.y >= grid[0].size() || grid[temp.x][temp.y]=='0') continue; q[r] = temp; grid[temp.x][temp.y] = '0'; r++; } l++; } sum++; } int numIslands(vector >& grid) { for(int x=0;x
转载地址:http://pnrai.baihongyu.com/