-
https://leetcode.cn/classic/problems/number-of-enclaves/description/
-
给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。
-
一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。
-
返回网格中 无法 在任意次数的移动中
离开网格边界的陆地单元格的数量
。
CODE
- 从外向内
class Solution {
public:
int numEnclaves(vector<vector<int>>& grid) {
int res = 0;
int m = grid.size();
int n = grid[0].size();
vector<vector<bool>> visit(m,vector<bool>(n,false));
for(int i=0;i<m;i++){
dfs(i,0,m,n,grid,visit);
dfs(i,n-1,m,n,grid,visit);
}
for(int i=0;i<n;i++){
dfs(0,i,m,n,grid,visit);
dfs(m-1,i,m,n,grid,visit);
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j] ==1)
if(!visit[i][j]){
res++;
}
}
}
return res;
}
bool dfs(int i,int j,int m,int n,vector<vector<int>>& grid,vector<vector<bool>>& visit){
if(i<0 || i>= m) return true;
if(j<0 || j>= n) return true;
if(grid[i][j] == 0){return false;}
if(visit[i][j]) return true;
visit[i][j] = true ;
bool up=false,down=false,left=false,right=false;
{ up = dfs(i-1,j,m,n,grid,visit);}
{ down = dfs(i+1,j,m,n,grid,visit);}
{ left = dfs(i,j-1,m,n,grid,visit);}
{ right = dfs(i,j+1,m,n,grid,visit);}
if(up || down || left || right ){
return true;
}
return false;
}
};
CG
-
leetcode200. 岛屿数量
-
错误示例
class Solution {
public:
int numEnclaves(vector<vector<int>>& grid) {
int res = 0;
int m = grid.size();
int n = grid[0].size();
vector<vector<bool>> visit(m,vector<bool>(n,false));
for(int i=0;i<m;i++){
if(grid[i][0] ==1)
dfs(i,0,m,n,grid,visit);
if(grid[i][n-1] ==1)
dfs(i,n-1,m,n,grid,visit);
}
for(int i=0;i<n;i++){
if(grid[0][i] ==1)
dfs(0,i,m,n,grid,visit);
if(grid[n-1][i] ==1)
dfs(m-1,i,m,n,grid,visit);
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j] ==1)
if(!visit[i][j]){
res++;
}
}
}
return res;
}
bool dfs(int i,int j,int m,int n,vector<vector<int>>& grid,vector<vector<bool>>& visit){
if(i<0 || i>= m) return true;
if(j<0 || j>= n) return true;
if(grid[i][j] == 0){return false;}
if(visit[i][j]) return true;
visit[i][j] = true ;
bool up=false,down=false,left=false,right=false;
{ up = dfs(i-1,j,m,n,grid,visit);}
{ down = dfs(i+1,j,m,n,grid,visit);}
{ left = dfs(i,j-1,m,n,grid,visit);}
{ right = dfs(i,j+1,m,n,grid,visit);}
if(up || down || left || right ){
return true;
}
return false;
}
};
- 超时(从内向外且没有记忆化搜索)
#include <stdio.h>
#include<vector>
#include<memory>
#include<iostream>
using namespace std;
class Solution {
public:
int numEnclaves(vector<vector<int>>& grid) {
int res = 0;
int m = grid.size();
int n = grid[0].size();
vector<vector<bool>> visit(n,vector<bool>(m));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j] ==1)
if(dfs(i,j,m,n,grid,visit)){
res++;
}
}
}
return res;
}
bool dfs(int i,int j,int m,int n,vector<vector<int>>& grid,vector<vector<bool>>& visit){
if(i<0 || i>= m) return true;
if(j<0 || j>= n) return true;
if(grid[i][j] == 0){return false;}
visit[i][j] = true ;
bool up,down,left,right;
if(i-1>=0)
if(not visit[i-1][j]){ up = dfs(i-1,j,m,n,grid,visit);}
if(i+1<m)
if(not visit[i+1][j]){ down = dfs(i+1,j,m,n,grid,visit);}
if(j-1>=0)
if(not visit[i][j-1]){ left = dfs(i,j-1,m,n,grid,visit);}
if(j+1<n)
if(not visit[i][j+1]){ right = dfs(i,j+1,m,n,grid,visit);}
visit[i][j] = false ;
if(up || down || left || right ){
return true;
}
return false;
}
};
int main()
{
printf("---<");
unique_ptr<Solution> mysolo = unique_ptr<Solution>(new Solution());
vector<vector<int>> gird = {{0,0,0,0},{1,0,1,0},{0,1,1,0},{0,0,0,0}};
int res = mysolo->numEnclaves(gird);
cout<< res<< endl;
return 0;
}