forked from starkblaze01/Algorithms-Cheatsheet-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_island.cpp
More file actions
71 lines (70 loc) · 1.57 KB
/
n_island.cpp
File metadata and controls
71 lines (70 loc) · 1.57 KB
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
//https://www.geeksforgeeks.org/find-number-of-islands/
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int l;
int issafe(vector<vector<int> > &ar,vector<vector<int> > &visited,int i,int j)
{
if(i>=0 && i<5 && j>=0 && j<5 && !visited[i][j] && ar[i][j] )
return 1;
return 0;
}
int dfs(vector<vector<int> > &ar,vector<vector<int> > &visited,int m,int n)
{
int xoff[8] = {-1,0,1,-1,1,-1,0,1};
int yoff[8] = {-1,-1,-1,0,0,1,1,1};
visited[m][n] = 1;
for(int k=0;k<8;k++)
{
if(issafe(ar,visited,xoff[k]+m,yoff[k]+n))
dfs(ar,visited,xoff[k]+m,yoff[k]+n);
}
}
int island(vector<vector<int> > ar)
{
//int visited[5][5];
//memset(visited, 0, sizeof(visited));
vector<vector<int> > visited(l,vector<int>(l,0));
int count =0;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(ar[i][j] && !visited[i][j])
{
dfs(ar,visited,i,j);
count++;
}
}
}
cout<<count;
}
int main()
{
int a[5][5]= { {1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1}
};
l = sqrt(sizeof(a)/sizeof(int));
vector <vector<int> > ar(l, vector<int>(l));
for(int i=0;i<l;i++)
{
for(int j=0;j<l;j++)
{
ar[i][j] = a[i][j];
}
}
/*for(int i=0;i<l;i++)
{
for(int j=0;j<l;j++)
{
cout<<ar[i][j]<<" ";
}
cout<<endl;
}*/
island(ar);
return 0;
}