Advertisement
xTheEc0

2. Marsaeigis (2014m. ITVBE)

May 26th, 2015
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. //2.txt
  2. /*
  3. 5 -1
  4. 8 -3
  5. 3
  6. 7 2 3 2 3 1 3 2
  7. 2 1 4
  8. 12 2 3 2 3 2 3 2 3 2 3 2 1
  9. */
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <fstream>
  13.  
  14. using namespace std;
  15.  
  16. // ----------------------------------------------------------------------------------------------
  17.  
  18. const char duomF[] = "2.txt";
  19. const char rezF[] = "2rez.txt";
  20. const int MaxSeku = 11;
  21. const int MaxIlgis = 35;
  22.  
  23. // ----------------------------------------------------------------------------------------------
  24.  
  25. struct marsaeigis {
  26.     int x;
  27.     int y;
  28.     };
  29.  
  30. // ----------------------------------------------------------------------------------------------
  31.  
  32. void skaitymas(marsaeigis A[], int &n, int k, int seka[][MaxIlgis])
  33. {
  34.     ifstream df(duomF);
  35.     df >> A[0].x >> A[0].y;
  36.     df >> A[1].x >> A[1].y;
  37.     df >> n;
  38.  
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         df >> k;
  42.         seka[i][0] = k;
  43.         for (int j = 1; j <= k; j++)
  44.         {
  45.             df >> seka[i][j];
  46.         }
  47.     }
  48. }
  49.  
  50. void sprendimas(marsaeigis A[], int n, int k, int seka[][MaxIlgis])
  51. {
  52.     int tempx, tempy;       // Kitamieji skirti laikinai saugoti pradines koordinates
  53.  
  54.     ofstream rf(rezF);      // Atidaromas failas
  55.     for (int i = 0; i < n; i++)
  56.     {
  57.         tempx = A[0].x;     // Perkeliame x koordinate i laikina vieta
  58.         tempy = A[0].y;     // Perkeliame y koordinate i laikina vieta
  59.         k = seka[i][0];
  60.         for (int j = 1; j <= k; j++)
  61.         {
  62.             if (seka[i][j] == 1) tempy += 1;    // Jeigu sekos reiksme yra 1, tai Y asyje judame i virsu
  63.             if (seka[i][j] == 2) tempx += 1;    // Jeigu sekos reiksme yra 2, tai X asyje judame i desine
  64.             if (seka[i][j] == 3) tempy -= 1;    // Jeigu sekos reiksme yra 3, tai Y asyje judame i apacia
  65.             if (seka[i][j] == 4) tempx -= 1;    // Jeigu sekos reiksme yra 4, tai X asyje judame i kaire
  66.             seka[i][0] = j;
  67.             if ((tempx == A[1].x) && (tempy == A[1].y)) break;  // Pasiekus tiksla, nutraukiamas ciklo darbas
  68.         }
  69.  
  70.         // Rasymas i faila
  71.         if ((tempx == A[1].x) && (tempy == A[1].y))
  72.         {
  73.             rf << fixed << left << setw(20) <<  "pasiektas tikslas" ;
  74.         }
  75.         else
  76.         {
  77.             rf << fixed << left << setw(20)<< "sekos pabaiga";
  78.         }
  79.  
  80.         for (int j = 1; j <= seka[i][0]; j++)
  81.         {
  82.             rf << seka[i][j] << " ";
  83.         }
  84.         rf << seka[i][0] << endl;
  85.     }
  86.  
  87.     rf.close();     // Uzdaromas failas
  88. }
  89.  
  90. // ----------------------------------------------------------------------------------------------
  91.  
  92. int main()
  93. {
  94.     marsaeigis A[2];            // Struktura skirta saugoti pradinem ir galinem koordinatem
  95.     int n, k;
  96.     int seka[MaxSeku][MaxIlgis];       // Dvimatis masyvas skirtas saugoti sekas
  97.  
  98.     skaitymas(A, n, k, seka);
  99.     sprendimas(A, n, k, seka);
  100.  
  101. // ----------------------------------------------------------------------------------------------
  102.  
  103.     /*Testavimas*/ {
  104.     /*
  105.     cout << x0 << " " << y0 << endl;
  106.     cout << x1 << " " << y1 << endl;
  107.     cout << n << endl;
  108.  
  109.     for (int i = 0; i < n; i++)
  110.     {
  111.         k = seka[i][0];
  112.         for (int j = 1; j <= k; j++)
  113.         {
  114.             cout << seka[i][j] << " ";
  115.         }
  116.         cout << " " << endl;
  117.     }
  118.     */
  119.     }
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement