Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream fin("labirint.in");
- const int N_MAX = 2005;
- const int M_MAX = 2005;
- int N, M;
- unsigned int poz_init_X, poz_init_Y;
- unsigned int poz_finala_X, poz_finala_Y;
- unsigned int matrice[N_MAX][M_MAX];
- const int directii = 4;
- const int di[directii] = {0, 0, 1, -1};
- const int dj[directii] = {1, -1, 0, };
- void Citire()
- {
- fin >> N >> M;
- fin >> poz_init_X >> poz_init_Y;
- fin >> poz_finala_X >> poz_finala_Y;
- for(int i = 0; i < N; i++)
- for(int j = 0; j < M; j++)
- fin >> matrice[i][j];
- }
- bool isOK(int i, int j)
- {
- if(i > N || i < 1)
- return false;
- if(j > M || j < 1)
- return false;
- if(matrice[i][j] == 1 || (i == poz_finala_X && j == poz_finala_Y))
- return true;
- if(matrice[i][j] == 0 || matrice[i][j])
- return false;
- return true;
- }
- void afisare()
- {
- for(int i = 0; i < N; i++)
- {
- for(int j = 0; j < M; j++)
- cout << matrice[i][j] << " ";
- cout << "\n";
- }
- cout << "\n\n";
- }
- void Rezolvare(int i, int j, int pas)
- {
- if(i == poz_finala_X && j == poz_finala_Y)
- afisare();
- else
- {
- for(int k = 0; k < directii; k++)
- {
- int i_vecin = i + di[k];
- int j_vecin = j + dj[k];
- if(isOK(i_vecin, j_vecin))
- {
- matrice[i_vecin][j_vecin] = pas;
- Rezolvare(i_vecin, j_vecin, pas + 1);
- matrice[i_vecin][j_vecin] = 0;
- }
- }
- }
- }
- int main()
- {
- Citire();
- Rezolvare(poz_init_X, poz_init_Y, 2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement