-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbscan.cpp
152 lines (150 loc) · 2.15 KB
/
dbscan.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Author: @himkha_100
Himanshu Khandelwal, NITW
*/
#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long int
#define s(t) scanf("%d",&t)
#define p(t) printf("%d\n",t)
#define pb push_back
#define f(t) for(int i=0;i<t;i++)
#define fi first
#define se second
#define all(t) t.begin(),t.end()
#define ci(t) cin>>t
#define co(t) cout<<t
#define mii map<int,int>
#define pii pair<int,int>
#define eps 5
#define siz 100
#define minp 2
using namespace std;
struct node{
int x;
int y;
};
inline bool operator<(const node& lhs, const node& rhs)
{
return lhs.x < rhs.x;
}
bool ac(int x,int y)
{
return x>y;
}
node d[siz];
string ns(int n)
{
string r;
while(n>0)
{
r=char((n%10)+48)+r;
n/=10;
}
return r;
}
map<node, int> vis;
double dist(node a,node b)
{
double x1,x2,y1,y2;
x1=a.x;
x2=b.x;
y1=a.y;
y2=b.y;
return sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
}
bool eps_neigh(node d1)
{
int cnt=0;
f(siz)
{
if(dist(d1,d[i])<=2.8)
{
cnt++;
}
}
if(cnt<eps) return false;
else return true;
}
vector<node> neigh(node d1)
{
vector<node> ans;
f(siz)
{
if(dist(d1,d[i])<=2.8&&vis[d[i]]==0)
{
ans.pb(d[i]);
}
}
return ans;
}
int main()
{
ifstream in;
in.open("f17.txt");
f(siz)
{
in>>d[i].x;
in>>d[i].y;
}
f(siz)
{
vis[d[i]]=0;
}
map<int,vector<node> > cl;
map<node,int> cn;
int c=0;
for(int r1=0;r1<siz;r1++)
{
if(vis[d[r1]]==0)
{
vis[d[r1]]=1;
if(eps_neigh(d[r1]))
{
c++;
cn[d[r1]]=c;
cl[c].pb(d[r1]);
vector<node> w=neigh(d[r1]);
f(w.size())
{
cn[w[i]]=c;
cl[c].pb(w[i]);
if(vis[w[i]]==0)
{
vis[w[i]]=1;
if(eps_neigh(w[i]))
{
vector<node> q=neigh(w[i]);
for(int z=0;z<q.size();z++)
{
w.pb(q[z]);
}
}
}
if(cn.find(w[i])==cn.end())
{
cn[w[i]]=c;
}
}
cout<<c<<endl;
ofstream out;
string st="fd"+ns(c);
st+=".txt";
out.open(st.c_str());
f(cl[c].size())
{
out<<cl[c][i].x<<" "<<cl[c][i].y<<endl;
cout<<cl[c][i].x<<" "<<cl[c][i].y<<endl;
}
out.close();
}
else
{
vis[d[r1]]=2;
}
}
}
cout<<c<<endl;
in.close();
return 0;
}