Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <queue>
- using namespace std;
- struct coord
- {
- int x, y;
- };
- queue <coord> q;
- int a[105][105], b[105][105], l, c;
- void Citire()
- {
- int i, j;
- ifstream fin("lee.in");
- fin >> l >> c;
- for(i = 1; i <= l; i++)
- for(j = 1; j <= c; j++)
- fin >> a[i][j];
- fin.close();
- }
- void Bordare()
- {
- int i;
- /// bordez coloanele 0 si c+1
- for(i = 0; i<= l + 1; i++)
- a[i][0] = a[i][c + 1] = 1;
- /// bordez liniile 0 si l+1
- for(i = 0; i <= c + 1; i++)
- a[0][i] = a[l + 1][i] = 1;
- }
- void Lee()
- {
- coord w, w1;
- w.x = 1;
- w.y = 1;
- q.push(w);
- b[1][1] = 1;
- while(!q.empty())
- {
- w = q.front();
- q.pop();
- /// nord
- w1.x = w.x - 1;
- w1.y = w.y;
- if(a[w1.x][w1.y] == 0 &&
- (b[w1.x][w1.y] == 0 || b[w1.x][w1.y] > b[w.x][w.y] + 1))
- {
- b[w1.x][w1.y] = b[w.x][w.y] + 1;
- q.push(w1);
- }
- /// sud
- w1.x = w.x + 1;
- w1.y = w.y;
- if(a[w1.x][w1.y] == 0 &&
- (b[w1.x][w1.y] == 0 || b[w1.x][w1.y] > b[w.x][w.y] + 1))
- {
- b[w1.x][w1.y] = b[w.x][w.y] + 1;
- q.push(w1);
- }
- /// est
- w1.x = w.x;
- w1.y = w.y + 1;
- if(a[w1.x][w1.y] == 0 &&
- (b[w1.x][w1.y] == 0 || b[w1.x][w1.y] > b[w.x][w.y] + 1))
- {
- b[w1.x][w1.y] = b[w.x][w.y] + 1;
- q.push(w1);
- }
- /// vest
- w1.x = w.x;
- w1.y = w.y - 1;
- if(a[w1.x][w1.y] == 0 &&
- (b[w1.x][w1.y] == 0 || b[w1.x][w1.y] > b[w.x][w.y] + 1))
- {
- b[w1.x][w1.y] = b[w.x][w.y] + 1;
- q.push(w1);
- }
- }
- }
- void Afisare()
- {
- int i, j;
- for(i = 1; i <= l; i++)
- {
- for(j = 1; j <= c; j++)
- cout << b[i][j] << " ";
- cout << "\n";
- }
- }
- int main()
- {
- Citire();
- Bordare();
- Lee();
- Afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment