Advertisement
a53

labirint5

a53
Mar 23rd, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream fin("labirint.in");
  5. ofstream fout("labirint.out");
  6.  
  7. char ch;
  8. int a[1001][1001], b[1001][1001], c[1001][1001];
  9. int N, M;
  10.  
  11. void Lee(int x, int y, int a[][1001])
  12. {
  13.  
  14. int l[] = {-1, 1, 0, 0}, c[] = {0, 0, -1, 1};
  15. queue <pair <int, int>> q;
  16. q.push({x, y});
  17. a[x][y] = 2;
  18. while(!q.empty())
  19. {
  20. int i = q.front().first, j = q.front().second;
  21. q.pop();
  22. for(int d = 0; d < 4; d++)
  23. {
  24. int ii = i + l[d], jj = j + c[d];
  25. if(ii < 1 || ii > N || jj < 1 || jj > M)
  26. continue;
  27.  
  28. if(a[ii][jj] == -1)
  29. {
  30. a[ii][jj] = a[i][j] + 1;
  31. continue;
  32. }
  33.  
  34. if(a[ii][jj] == 0)
  35. {
  36. a[ii][jj] = a[i][j] + 1;
  37. q.push({ii, jj});
  38. }
  39. }
  40. }
  41. }
  42.  
  43. int main()
  44. {
  45.  
  46. fin>>N>>M;
  47. fin.get();
  48. for(int i = 1; i <= N; i++)
  49. {
  50. for(int j = 1; j <= M; j++)
  51. {
  52. fin>>ch;
  53. a[i][j] = (ch == '0')? 0 : 1, b[i][j] = c[i][j] = -a[i][j];
  54. }
  55. }
  56. Lee(1, 1, b);
  57. Lee(N, M, c);
  58. for(int i = 1; i <= N; i++)
  59. {
  60. for(int j = 1; j <= M; j++)
  61. if(a[i][j] == 1 && b[i][j] + c[i][j] - 1 < b[N][M] && b[i][j] + c[i][j] - 1 >= 0)
  62. fout.put('1');
  63. else
  64. fout.put('0');
  65. fout<<'\n';
  66. }
  67. return 0;
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement