Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1010;
  6. int a[N][N];
  7. int n, x_1, y_1, x_2, y_2;
  8.  
  9. bool bounds(int i, int j) {
  10. return i > 0 && j > 0 && j < n + 1 && i < n + 1;
  11. }
  12.  
  13. void inv(int i, int j) {
  14. a[i][j] = -abs(a[i][j]);
  15. }
  16.  
  17. int main() {
  18. freopen("in.txt", "r", stdin);
  19. freopen("out.txt", "w", stdout);
  20. ios::sync_with_stdio(false);
  21. cin >> n >> x_1 >> y_1 >> x_2 >> y_2;
  22. for(int i = 1; i < n + 1; ++i) {
  23. for(int j = 1; j < n + 1; ++j) {
  24. cin >> a[i][j];
  25. }
  26. }
  27. queue<pair<int, int>> q;
  28. q.push(make_pair(x_1, y_1));
  29. while(!q.empty()) {
  30. auto x = q.front().first;
  31. auto y = q.front().second;
  32. inv(x, y);
  33. q.pop();
  34. if(bounds(x - 1, y) && abs(a[x - 1][y]) <= abs(a[x][y]) && a[x - 1][y] > 0) {
  35. inv(x - 1, y);
  36. q.push(make_pair(x - 1, y));
  37. }
  38. if(bounds(x + 1, y) && abs(a[x + 1][y]) <= abs(a[x][y]) && a[x + 1][y] > 0) {
  39. inv(x + 1, y);
  40. q.push(make_pair(x + 1, y));
  41. }
  42. if(bounds(x, y - 1) && abs(a[x][y - 1]) <= abs(a[x][y]) && a[x][y - 1] > 0) {
  43. inv(x, y - 1);
  44. q.push(make_pair(x, y - 1));
  45. }
  46. if(bounds(x, y + 1) && abs(a[x][y + 1]) <= abs(a[x][y]) && a[x][y + 1] > 0) {
  47. inv(x, y + 1);
  48. q.push(make_pair(x, y + 1));
  49. }
  50. }
  51. for(int i = 1; i < n + 1; ++i) {
  52. for(int j = 1; j < n + 1; ++j) {
  53. if(a[i][j] < 0) {
  54. cout << -a[i][j] << " ";
  55. } else {
  56. cout << "0 ";
  57. }
  58. }
  59. cout << "\n";
  60. }
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement