注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
我把自己连猜带蒙写的Solution都贴出来了,不知道下周会不会HR联系我
class Point {
public:
int x, y;
int value;
int id;
Point(int x, int y, int value=0, int id=0) {
this->x=x;
this->y=y;
this->value=value;
this->id=id;
}
};
void test() {
vector<vector<Point*>> points;
points.push_back({});
points[0].push_back(new Point(0, 0, 1));
points[0].push_back(new Point(0, 1, 1));
points[0].push_back(new Point(0, 2));
points[0].push_back(new Point(0, 3));
points[0].push_back(new Point(0, 4));
points.push_back({});
points[1].push_back(new Point(1, 0, 1));
points[1].push_back(new Point(1, 1, 1));
points[1].push_back(new Point(1, 2));
points[1].push_back(new Point(1, 3));
points[1].push_back(new Point(1, 4));
points.push_back({});
points[2].push_back(new Point(2, 0));
points[2].push_back(new Point(2, 1));
points[2].push_back(new Point(2, 2, 1));
points[2].push_back(new Point(2, 3));
points[2].push_back(new Point(2, 4));
points.push_back({});
points[3].push_back(new Point(3, 0));
points[3].push_back(new Point(3, 1));
points[3].push_back(new Point(3, 2));
points[3].push_back(new Point(3, 3, 1));
points[3].push_back(new Point(3, 4, 1));
points.push_back({});
points[4].push_back(new Point(4, 0));
points[4].push_back(new Point(4, 1));
points[4].push_back(new Point(4, 2));
points[4].push_back(new Point(4, 3, 1));
points[4].push_back(new Point(4, 4, 1));
/*
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 1 1
*/
vector<vector<Point*>> result = pixelClustering(points, 1, 3);
}
vector<vector<Point*>> pixelClustering(vector<vector<Point*>>& points, int dist, int counts) {
if (points.empty() || points.size()==0 || points[0].size()==0) {
return points;
}
vector<vector<Point*>> result=points;
int newid=1;
for (int i=0; i<result.size(); i++) {
for (int j=0; j<result[0].size(); j++) {
if (result[i][j]->value==1) {
int counter=1;
dfsHelper(points, i, j, dist, counter);
if (counter<counts) {
cleaner(points, i, j, dist);
}
else {
remarker(points, i, j, dist, newid);
newid++;
}
}
}
}
return result;
}
private:
void remarker(vector<vector<Point*>>& points, int x, int y, int dist, int newid) {
points[x][y]->value=2;
points[x][y]-&ged_map<string, vector<string>> graph, unordered_map<string, bool>& available, vector<string>& result) {
unordered_map<string, int> state;
for (auto it = graph.begin(); it != graph.end(); ++it) {
state[it->first]=0;
}
for (auto it = graph.begin(); it != graph.end(); ++it) {
if (available[it->first]==true) {
visit(graph, it->first, result, state);
}
}
return;
}
void visit(unordered_map<string, vector<string>>& image,
string it,
vector<string>& list,
unordered_map<string, int>& state) {
if (state[it]==3) {
return;
}
if (state[it]==2) {
return;
}
state[it]=2;
for (auto neib:image[it]) {
visit(image, neib, list, state);
}
state[it]=3;
list.push_back(it);
}
void buildGraph(vector<vector<string>>& image, unordered_map<string, vector<string>>& graph) {
for (int i=0; i<image.size(); i++) {
graph[image[i][0]].push_back(image[i][1]);
}
}
bool isCyclicHelper(string v, unordered_map<string, vector<string>>& graph, unordered_map<string, bool>& visited, unordered_map<string, bool>& recStack) {
if (visited[v]==false) {
visited[v]=true;
recStack[v]=true;
for (auto it=graph[v].begin(); it!=graph[v].end(); ++it) {
if (!visited[*it] && isCyclicHelper(*it, graph, visited, recStack)) {
return true;
}
else if (recStack[*it]==true) {
return true;
}
}
}
recStack[v]=false;
return false;
}
|