用set记录形状
class Solution {public: int numIslands(vector>& grid) { int width = grid.size(); if(width <= 0) return 0; int length = grid[0].size(); if(length <= 0) return 0; set >>> shapes; for(int i = 0;i < width;i++){ for(int j = 0;j < length;j++){ if(grid[i][j] == '1'){ vector > shape; search(); shapes.insert(shape); } } } return shapes.size(); } void search(vector >& grid,int sx,int sy,int x,int y,vector >& shape){ if(x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[i][j] == '0') return; grid[x][y] == '0'; shape.push_back({sx-x,sy-y}); search(grid,sx,sy,x-1,y,shape); search(grid,sx,sy,x+1,y,shape); search(grid,sx,sy,x,y-1,shape); search(grid,sx,sy,x,y+1,shape); }};
https://blog.csdn.net/magicbean2/article/details/79248704