Created
January 31, 2012 22:37
-
-
Save SebastianTroc/1713507 to your computer and use it in GitHub Desktop.
ComiVojager problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Problem komiwojazera | |
| #include <iostream> | |
| #include <cstdlib> | |
| #include <math.h> | |
| using namespace std; | |
| int const ILOSC_MIAST = 10; | |
| class miasto { | |
| public: | |
| int ID; | |
| int X; | |
| int Y; | |
| bool odwiedzone; | |
| miasto() { | |
| odwiedzone = false; | |
| } | |
| void Odwiedz() { | |
| odwiedzone = true; | |
| } | |
| }; | |
| float Odleglosc(int x1, int y1, int x2, int y2) { | |
| float odleglosc = sqrt( pow(x1 - x2, 2) + pow(y1 - y2, 2) ); | |
| return odleglosc; | |
| } | |
| int jestem_w, najblizsze; | |
| int main() | |
| { | |
| miasto miasta[ILOSC_MIAST]; | |
| //losowanie wspolrzednych miast | |
| srand(time(NULL)); | |
| for (short i = 0; i<ILOSC_MIAST; i++ ) { | |
| miasta[i].ID = i+1; | |
| miasta[i].X = (rand() % 100) + 1; | |
| miasta[i].Y = (rand() % 101) + 1; | |
| } | |
| //wyswietlenie miast | |
| for (short i = 0; i<ILOSC_MIAST; i++ ) { | |
| cout << "Wspolrzedne miasta " << miasta[i].ID << " = [" << miasta[i].X << "," << miasta[i].Y << "]" << endl; | |
| } | |
| //z jakiego miasta wyjezdzamy? | |
| int start_city; | |
| do { | |
| cout << "Z jakiego miasta chcesz wyjechac? "; | |
| cin >> start_city; | |
| cout << endl; | |
| } while (start_city > ILOSC_MIAST || start_city < 1); | |
| jestem_w = start_city-1; // -1, bo tablica idzie od zera | |
| //oznaczamy wybrany punkt startowy jako odwiedzony | |
| miasta[jestem_w].odwiedzone = true; | |
| cout << "Jestes w miescie " << miasta[jestem_w].ID << endl; | |
| //deklaracja tablicy zapisujacej kolejne kroki podrozy | |
| //(potrzebnej do pozniejszego wyswietlenia podsumowania) | |
| int sugerowana_trasa[ILOSC_MIAST]; | |
| //GLOWNA PETLA | |
| for (short i = 0; i<ILOSC_MIAST-1; i++ ) { | |
| //Wypelniane tablicy z podsumowaniem podrozy | |
| if (i == 0) { | |
| sugerowana_trasa[i] = start_city; | |
| } else { | |
| sugerowana_trasa[i] = miasta[jestem_w].ID; | |
| } | |
| float min = 99999999999.0; | |
| for (short j = 0; j<ILOSC_MIAST; j++ ) { | |
| //dokonywanie obliczen tylko dla nieodwiedzonych miast | |
| if (miasta[j].odwiedzone == false) { | |
| float odleglosc_robocza = Odleglosc(miasta[jestem_w].X, miasta[jestem_w].Y, miasta[j].X, miasta[j].Y ); | |
| cout << "- Odleglosc do miasta " << miasta[j].ID << " wynosi: " << odleglosc_robocza << endl; | |
| //Szukanie najblizszego miasta | |
| if (odleglosc_robocza < min) { | |
| min = odleglosc_robocza; | |
| najblizsze = j; | |
| } | |
| } | |
| } | |
| // | |
| jestem_w = najblizsze; | |
| //Informacja o powyzszej operacji | |
| cout << "\n Najblizej jest do miasta " << miasta[najblizsze].ID << ", wiec jade tam!" << endl << endl; | |
| //Uzycie funkcji klasy | |
| miasta[jestem_w].Odwiedz(); | |
| } //KONIEC GLOWNEJ PETLI | |
| //Wypelnienie ostatniego elementu tablicy z sugerowana trasa | |
| sugerowana_trasa[ILOSC_MIAST-1] = miasta[jestem_w].ID; | |
| //Odtworzenie podrozy - sugerowana trasa | |
| cout << "\n\nPlan podrozy obliczony przez program:" << endl; | |
| for (short i = 0; i<ILOSC_MIAST; i++) { | |
| cout << sugerowana_trasa[i]; | |
| if (i < ILOSC_MIAST-1) { | |
| cout << " => "; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment