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 "head.h"
class Solution{
public:
int rank2island(vector<vector<char> > &board){
int m = board.size();
if(!m) return 0;
int n = board[0].size();
if(!n) return 0;
// PrintVV(board, "board: the original board");
queue<pair<int ,int>> que;
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(board[i][j]=='0' && (i==0 || j==0 || i==m-1 || j==n-1)){
board[i][j] = '2';
que.push(pair<int, int>(i, j));
}
}
}
while(!que.empty()){
int i = que.front().first, j = que.front().second;
que.pop();
bfs(board, i, j, m, n, que, '0', '2');
}
// PrintVV(board, "\nboard:set ocean to 2");//2 is ocean
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(board[i][j]=='2'){
bfs(board, i, j, m, n, que, '1', '3');
}
}
}
while(!que.empty()){
int i = que.front().first, j = que.front().second;
que.pop();
bfs(board, i, j, m, n, que, '1', '3');
}
// PrintVV(board, "\nboard:set continent to 3");
int cnt = 0;
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(board[i][j]=='1') cnt++;
}
}
// cout<<"\nthe number of rank 2 islands is "<<cnt<<endl;
return cnt;
}
void bfs(vector<vector<char>> &board, int i, int j, int m, int n, queue<pair<int, int>> &que, char a, char b){
if(i-1>=0 && board[i-1][j]==a){board[i-1][j]=b; que.push(pair<int, int>(i-1, j));}
if(i+1<m && board[i+1][j]==a){board[i+1][j]=b; que.push(pair<int, int>(i+1, j));}
if(j-1>=0 && board[i][j-1]==a){board[i][j-1]=b; que.push(pair<int, int>(i, j-1));}
if(j+1<n && board[i][j+1]==a){board[i][j+1]=b; que.push(pair<int, int>(i, j+1));}
if(i-1>=0 && j-1>=0 && board[i-1][j-1]==a){board[i-1][j-1]=b; que.push(pair<int, int>(i-1, j-1));}
if(i-1>=0 && j+1<n && board[i-1][j+1]==a){board[i-1][j+1]=b; que.push(pair<int, int>(i-1, j+1));}
if(i+1<m && j-1>=0 && board[i+1][j-1]==a){board[i+1][j-1]=b; que.push(pair<int, int>(i+1, j-1));}
if(i+1<m && j+1<n && board[i+1][j+1]==a){board[i+1][j+1]=b; que.push(pair<int, int>(i+1, j+1));}
}
};
int main(){
vector<vector<char> > board1{{'0','0','0','0','0','0','0','0','0'},\
{'0','0','0','0','0','1','1','0','0'},\
{'0','0','1','1','1','1','1','0','0'},\
{'0','1','1','0','0','0','1','0','0'},\
{'0','0','1','0','1','0','1','0','0'},\
{'0','0','1','0','0','0','1','0','0'},\
{'0','0','1','1','1','1','1','0','0'},\
{'0','0','0','0','0','0','0','0','0'}};
vector<vector<char> > board2{{'0','0','1','0','0','0','0','0','0'},\
{'0','1','1','1','1','1','0','0','0'},\
{'0','1','0','0','0','1','0','0','0'},\
{'0','1','0','1','0','1','0','0','0'},\
{'0','1','0','0','0','1','0','0','0'},\
{'0','1','1','1','1','1','0','0','0'},\
{'0','0','0','0','0','0','0','0','0'},\
{'0','0','0','0','0','0','0','0','0'},\
{'0','1','1','1','1','1','0','0','0'},\
{'0','1','0','0','0','1','0','0','0'},\
{'0','1','0','1','0','1','0','0','0'},\
{'0','1','0','0','0','1','0','0','0'},\
{'0','1','1','1','1','1','0','0','0'},\
{'0','0','0','0','0','0','0','0','0'}};
Solution sol;
sol.rank2island(board2);
return 0;
}
|