Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1010;
- int a[N][N];
- int n, x_1, y_1, x_2, y_2;
- bool bounds(int i, int j) {
- return i > 0 && j > 0 && j < n + 1 && i < n + 1;
- }
- void inv(int i, int j) {
- a[i][j] = -abs(a[i][j]);
- }
- int main() {
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- ios::sync_with_stdio(false);
- cin >> n >> x_1 >> y_1 >> x_2 >> y_2;
- for(int i = 1; i < n + 1; ++i) {
- for(int j = 1; j < n + 1; ++j) {
- cin >> a[i][j];
- }
- }
- queue<pair<int, int>> q;
- q.push(make_pair(x_1, y_1));
- while(!q.empty()) {
- auto x = q.front().first;
- auto y = q.front().second;
- inv(x, y);
- q.pop();
- if(bounds(x - 1, y) && abs(a[x - 1][y]) <= abs(a[x][y]) && a[x - 1][y] > 0) {
- inv(x - 1, y);
- q.push(make_pair(x - 1, y));
- }
- if(bounds(x + 1, y) && abs(a[x + 1][y]) <= abs(a[x][y]) && a[x + 1][y] > 0) {
- inv(x + 1, y);
- q.push(make_pair(x + 1, y));
- }
- if(bounds(x, y - 1) && abs(a[x][y - 1]) <= abs(a[x][y]) && a[x][y - 1] > 0) {
- inv(x, y - 1);
- q.push(make_pair(x, y - 1));
- }
- if(bounds(x, y + 1) && abs(a[x][y + 1]) <= abs(a[x][y]) && a[x][y + 1] > 0) {
- inv(x, y + 1);
- q.push(make_pair(x, y + 1));
- }
- }
- for(int i = 1; i < n + 1; ++i) {
- for(int j = 1; j < n + 1; ++j) {
- if(a[i][j] < 0) {
- cout << -a[i][j] << " ";
- } else {
- cout << "0 ";
- }
- }
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement