-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number of Islands.cpp
92 lines (85 loc) · 1.94 KB
/
Number of Islands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<string>
#include<vector>
#include <cctype>
#include<algorithm>
#include<math.h>
using namespace std ;
typedef struct ListNode{
int val;
ListNode * next;
} ListNode;
class Solution {
public:
int numIslands(vector<vector<char> >& grid) {
int row=grid.size();
if(!row)
return 0;
int col=grid[0].size();
vector<int> temp(col,0);
vector<vector<int> > flag(row,temp);
int i,j,res=0;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(grid[i][j]=='1'&&flag[i][j]==0){
write_flag(flag,grid,i,j);
res++;
}
}
}
return res;
}
void write_flag(vector<vector<int> > &flag,vector<vector<char> >&grid,int i,int j){
flag[i][j]=1;
//cout<<i<<" "<<j<<endl;
if(i>0&&grid[i-1][j]=='1'&&flag[i-1][j]==0)
write_flag(flag,grid,i-1,j);
if(i+1<grid.size()&&grid[i+1][j]=='1'&&flag[i+1][j]==0)
write_flag(flag,grid,i+1,j);
if(j>0&&grid[i][j-1]=='1'&&flag[i][j-1]==0)
write_flag(flag,grid,i,j-1);
if(j+1<grid[0].size()&&grid[i][j+1]=='1'&&flag[i][j+1]==0)
write_flag(flag,grid,i,j+1);
}
void print()
{
vector<vector<char> > A;
vector<char> x;
x.push_back('1');
x.push_back('1');
x.push_back('0');
x.push_back('0');
x.push_back('0');
A.push_back(x);
x.clear();
x.push_back('1');
x.push_back('1');
x.push_back('0');
x.push_back('0');
x.push_back('0');
A.push_back(x);
x.clear();
x.push_back('0');
x.push_back('0');
x.push_back('1');
x.push_back('0');
x.push_back('0');
A.push_back(x);
x.clear();
x.push_back('0');
x.push_back('0');
x.push_back('0');
x.push_back('1');
x.push_back('1');
A.push_back(x);
cout<<numIslands(A)<<endl;
}
};
int main()
{
Solution test;
// cout<<test.Low("A man, a plan, a canal: Panama")<<endl;
test.print();
// cout<<atoi("2147483648")<<endl;
system("pause");
}