Island Perimeter

来源:互联网 发布:软件项目估算表 编辑:程序博客网 时间:2024/05/18 08:35

勉强算一道搜索吧。。

class Solution {public:    int islandPerimeter(vector<vector<int>>& grid) {        int row = grid.size();        int col = grid[0].size();        int sum = 0;        for(int i=0 ;i<row ; i++){            for(int j=0 ;j<col ; j++){                if(grid[i][j] == 1){                    sum += cal(grid,i,j,row,col);                }            }        }        return sum;    }        int cal(vector<vector<int>>& grid,int x,int y,int row,int col){        int count = 0;        int fx[4][2] = {0,1,0,-1,1,0,-1,0};        for(int i=0 ;i<4; i++){            int nx = x + fx[i][0];            int ny = y + fx[i][1];            if(nx<0 || nx >=row || ny<0 || ny>=col){                count ++;            }            else if(nx>=0 && nx<row && ny>=0 && ny<col ){                if(grid[nx][ny] == 0){                    count ++;                }            }        }        return count;    }};


原创粉丝点击