Adding medium/hard puzzles & challenges...

This commit is contained in:
Xavier Morel
2017-04-06 16:49:53 +02:00
parent 50a3316969
commit 929c5f1912
24 changed files with 2930 additions and 1 deletions

View File

@@ -0,0 +1,74 @@
// https://www.codingame.com/ide/puzzle/teads-sponsored-contest
// Xavier Morel - 2016-03-17
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <cmath>
using namespace std;
map<int, vector<int>> points;
int findFarthestNode(int origin, int *distanceOut)
{
int maxDistance = 0;
int farthestNode = 0;
vector<pair<int,int>> process;
set<int> discovered;
process.push_back(pair<int,int>(origin,0));
while (process.size() > 0) {
pair<int,int> node = process.back(); // node, distance
process.pop_back();
discovered.insert(node.first);
if (node.second > maxDistance) {
maxDistance = node.second;
farthestNode = node.first;
}
for (int adjNode : points[node.first]) {
if (discovered.find(adjNode) == discovered.end()) {
process.push_back(pair<int,int>(adjNode, node.second + 1));
}
}
}
if (distanceOut != 0) {
*distanceOut = maxDistance;
}
return farthestNode;
}
int main()
{
int n;
cin >> n; cin.ignore();
int firstNode;
for (int i = 0; i < n; i++) {
int xi;
int yi;
cin >> xi >> yi; cin.ignore();
points[xi].push_back(yi);
points[yi].push_back(xi);
if (i == 0) {
firstNode = xi;
}
}
int distance;
int farNode = findFarthestNode(firstNode, &distance);
cerr << "Farthest node from origin is " << farNode << " with distance of " << distance << endl;
farNode = findFarthestNode(farNode, &distance);
cerr << "Farthest node from previous is " << farNode << " with distance of " << distance << endl;
cout << ceil(distance / 2.0f) << endl;
}