yeasir007

Showing posts with label Implement without STL. Show all posts
Showing posts with label Implement without STL. Show all posts

Wednesday, February 14, 2018

Divisible Sum Pairs

Divisible Sum Pairs

Friday, February 02, 2018

216 Getting in Line

216 Getting in Line























































































Solution:

#include<iostream>
#include <stdio.h>
#include<math.h>
#include <iomanip>

using namespace std;

#define max_network_range 10
int NUM_OF_NETWORKS;
double MIN_WIRE_COST = 0.00;
struct NetworkDetails
{
    int x;
    int y;
}networksDetails[max_network_range], networkTaken[max_network_range];


int output[max_network_range];
int minOutPutPattern[max_network_range];
bool used[max_network_range];

void initUsed();
void permutation(int level,int taken);
void printPermutation();
void displayOutput(int TC);
double calculateDistance();
double calcDistBetnTwoPoint(int x1,int y1,int x2,int y2);

int main()
{
    freopen("216.txt", "r", stdin);

    std::cout << std::setprecision(2) << std::fixed;

    NUM_OF_NETWORKS = 0;
    int TC = 0;
    while (cin >> NUM_OF_NETWORKS && NUM_OF_NETWORKS !=0)
    {
        for (int i = 0; i < NUM_OF_NETWORKS; i++)
        {
            cin >> networksDetails[i].x >> networksDetails[i].y;
        }

        initUsed();
        MIN_WIRE_COST = 9999999.00;
        permutation(0, NUM_OF_NETWORKS);

        displayOutput(++TC);
    }

    return 0;
}

void permutation(int level, int taken)
{
    if (level == taken)
    {
        double cost = calculateDistance();
        if (MIN_WIRE_COST > cost)
        {
            MIN_WIRE_COST = cost;
            for (int i = 0; i < NUM_OF_NETWORKS; i++)
            {
                minOutPutPattern[i] = output[i];
            }
        }

        return;
    }

    for (int i = 0; i < NUM_OF_NETWORKS; i++)
    {
        if (!used[i])
        {
            used[i] = true;
            output[level] = i;
            permutation(level + 1,taken);
            used[i] = false;
        }
    }
}

void displayOutput(int TC)
{
    cout << "**********************************************************" << endl;
    cout << "Network #" << TC << endl;

    for (int i = 1; i < NUM_OF_NETWORKS; i++)
    {
        double res = 0.0;
        res = calcDistBetnTwoPoint(networksDetails[minOutPutPattern[i - 1]].x, networksDetails[minOutPutPattern[i - 1]].y, networksDetails[minOutPutPattern[i]].x, networksDetails[minOutPutPattern[i]].y);
        cout << "Cable requirement to connect (" << networksDetails[minOutPutPattern[i - 1]].x << "," << networksDetails[minOutPutPattern[i - 1]].y << ") to (" << networksDetails[minOutPutPattern[i]].x << "," << networksDetails[minOutPutPattern[i]].y << ") is " << res << " feet." << endl;
    }

    cout << "Number of feet of cable required is " << MIN_WIRE_COST << "." << endl;
}

double calculateDistance()
{
    double distance = 0.00;
    for (int i = 1; i <NUM_OF_NETWORKS; i++)
    {
        distance += calcDistBetnTwoPoint(networksDetails[output[i - 1]].x, networksDetails[output[i - 1]].y, networksDetails[output[i]].x, networksDetails[output[i]].y);
    }

    return distance;
}

double calcDistBetnTwoPoint(int x1, int y1, int x2, int y2)
{
    double res = 0.0;
    res = (16.0 + sqrt(pow((double)(abs(x1 - x2)), 2.0) + pow((double)(abs(y1 - y2)), 2.0)));
    return res;
}

void initUsed()
{
    for (int i = 0; i < max_network_range; i++)
    {
        used[i] = false;
        output[i] = 0;
        minOutPutPattern[i] = 0;
    }
}