Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //2.txt
- /*
- 5 -1
- 8 -3
- 3
- 7 2 3 2 3 1 3 2
- 2 1 4
- 12 2 3 2 3 2 3 2 3 2 3 2 1
- */
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- // ----------------------------------------------------------------------------------------------
- const char duomF[] = "2.txt";
- const char rezF[] = "2rez.txt";
- const int MaxSeku = 11;
- const int MaxIlgis = 35;
- // ----------------------------------------------------------------------------------------------
- struct marsaeigis {
- int x;
- int y;
- };
- // ----------------------------------------------------------------------------------------------
- void skaitymas(marsaeigis A[], int &n, int k, int seka[][MaxIlgis])
- {
- ifstream df(duomF);
- df >> A[0].x >> A[0].y;
- df >> A[1].x >> A[1].y;
- df >> n;
- for (int i = 0; i < n; i++)
- {
- df >> k;
- seka[i][0] = k;
- for (int j = 1; j <= k; j++)
- {
- df >> seka[i][j];
- }
- }
- }
- void sprendimas(marsaeigis A[], int n, int k, int seka[][MaxIlgis])
- {
- int tempx, tempy; // Kitamieji skirti laikinai saugoti pradines koordinates
- ofstream rf(rezF); // Atidaromas failas
- for (int i = 0; i < n; i++)
- {
- tempx = A[0].x; // Perkeliame x koordinate i laikina vieta
- tempy = A[0].y; // Perkeliame y koordinate i laikina vieta
- k = seka[i][0];
- for (int j = 1; j <= k; j++)
- {
- if (seka[i][j] == 1) tempy += 1; // Jeigu sekos reiksme yra 1, tai Y asyje judame i virsu
- if (seka[i][j] == 2) tempx += 1; // Jeigu sekos reiksme yra 2, tai X asyje judame i desine
- if (seka[i][j] == 3) tempy -= 1; // Jeigu sekos reiksme yra 3, tai Y asyje judame i apacia
- if (seka[i][j] == 4) tempx -= 1; // Jeigu sekos reiksme yra 4, tai X asyje judame i kaire
- seka[i][0] = j;
- if ((tempx == A[1].x) && (tempy == A[1].y)) break; // Pasiekus tiksla, nutraukiamas ciklo darbas
- }
- // Rasymas i faila
- if ((tempx == A[1].x) && (tempy == A[1].y))
- {
- rf << fixed << left << setw(20) << "pasiektas tikslas" ;
- }
- else
- {
- rf << fixed << left << setw(20)<< "sekos pabaiga";
- }
- for (int j = 1; j <= seka[i][0]; j++)
- {
- rf << seka[i][j] << " ";
- }
- rf << seka[i][0] << endl;
- }
- rf.close(); // Uzdaromas failas
- }
- // ----------------------------------------------------------------------------------------------
- int main()
- {
- marsaeigis A[2]; // Struktura skirta saugoti pradinem ir galinem koordinatem
- int n, k;
- int seka[MaxSeku][MaxIlgis]; // Dvimatis masyvas skirtas saugoti sekas
- skaitymas(A, n, k, seka);
- sprendimas(A, n, k, seka);
- // ----------------------------------------------------------------------------------------------
- /*Testavimas*/ {
- /*
- cout << x0 << " " << y0 << endl;
- cout << x1 << " " << y1 << endl;
- cout << n << endl;
- for (int i = 0; i < n; i++)
- {
- k = seka[i][0];
- for (int j = 1; j <= k; j++)
- {
- cout << seka[i][j] << " ";
- }
- cout << " " << endl;
- }
- */
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement