-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindIsland
134 lines (122 loc) · 3.24 KB
/
findIsland
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
int row[]={-1,-1,-1,0,0,1,1,1};
int col[]={-1,0,1,-1,1,-1,0,1};
bool isSafe(int i,int j,vector<int> A[],int N,int M,
vector<vector<bool>>visited){
return (i>=0 && j>=0 && i<N && j< M && !visited[i][j]
&& A[i][j]==1);
}
void findIsland(vector<int> A[], int i,int j,int N,int M,
vector<vector<bool>>&visited){
// visited[i][j]=true;
queue<pair<int,int>>q;
q.push(pair<int,int>(i,j));
while(!q.empty()){
pair<int,int>p = q.front();
visited[p.first][p.second]=true;
q.pop();
for(int k=0;k<8;++k){
int x = row[k]+p.first;
int y = col[k]+p.second;
if(isSafe(x,y,A,N,M,visited)){
q.push(pair<int,int>(x,y));
visited[x][y]=true;
// break;
//findIsland(A,x,y,N,M,visited);
}
}
}
}
int findIslands(vector<int> A[], int N, int M) {
int res=0;
vector<vector<bool>>visited(N);
for(int i=0;i<N;i++){
visited[i]= vector<bool>(M);
for(int j=0;j<M;j++)
visited[i][j]=false;
}
for(int i=0;i<N;++i){
for(int j=0;j<M;++j){
if(!visited[i][j] && A[i][j]==1){
findIsland(A,i,j,N,M,visited);
res++;
}
}
}
return res;
}
int row[]={-1,-1,-1,0,0,1,1,1};
int col[]={-1,0,1,-1,1,-1,0,1};
bool isSafe(int i,int j,vector<int> A[],int N,int M){
return (i>=0 && j>=0 && i<N && j<M && A[i][j]==1);
}
void findIsland(vector<int> A[], int i,int j,int N,int M){
//visited[i][j]=true;
A[i][j]=0;
// queue<pair<int,int>>q;
// q.push(pair<int,int>(i,j));
// while(!q.empty()){
// pair<int,int>p = q.front();
// visited[p.first][p.second]=true;
// q.pop();
for(int k=0;k<8;++k){
int x = row[k]+i;
int y = col[k]+j;
if(isSafe(x,y,A,N,M)){
// q.push(pair<int,int>(x,y));
// visited[x][y]=true;
// break;
findIsland(A,x,y,N,M);
}
}
// }
}
int findIslands(vector<int> A[], int N, int M) {
int res=0;
// vector<vector<bool>>visited(N);
// for(int i=0;i<N;i++){
// visited[i]= vector<bool>(M);
// for(int j=0;j<M;j++)
// visited[i][j]=false;
// }
for(int i=0;i<N;++i){
for(int j=0;j<M;++j){
if(A[i][j]==1){
findIsland(A,i,j,N,M);
res++;
}
}
}
return res;
}
int* topoSort(int V, vector<int> adj[]) {
int cnt=0;
int *arr=new int[V];
if(V==0)
return arr;
int inDeg[V]={0};
for(int i=0;i<V;++i){
for(auto it:adj[i])
inDeg[it]++;
}
queue<int>q;
for(int i=0;i<V;++i){
if(inDeg[i]==0)
q.push(i);
}
while(!q.empty()){
int v = q.front();
arr[cnt++]=v;
q.pop();
for(int i=0;i<adj[v].size();++i){
if(--inDeg[adj[v][i]]==0)
q.push(adj[v][i]);
}
}
return arr;
}
int main()
{
cout<<"Hello World";
return 0;
}