Header Ad

HackerEarth Missile Bombing problem solution

In this HackerEarth Missile Bombing problem solution, There is an N x N field on which missiles are being bombarded. Initially, all the cells in this field have 0 value. There will be M missiles bombarded on this field. the ith missile will have power Pi and it will affect all the cells in the region with (Ai, Bi) as top-left corner and (Ci, Di) as a bottom-right corner. Because of the missile, the value of all the cells in this rectangle will get XOR with Pi.

After all the missiles have been bombarded, you have to find out values in each cell of this field.


HackerEarth Missile Bombing problem solution


HackerEarth Missile Bombing problem solution.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fr freopen("in.txt","r",stdin)
#define rep(i,n) for(int i=0;i<n;i++)
#define frep(i,n) for(int i=1;i<=n;i++)

#define pi pair<int,int>
#define f first
#define s second
#define MAXBITS 15
int U[1011][1011];
int main() {

int N;
cin >> N;

int M;
cin >> M;
int a,b,c,d,p;
rep(i,M) {
cin >> p >> a >> b >> c >> d;
U[a][b]^=p;
U[a][d+1]^=p;
U[c+1][b]^=p;
U[c+1][d+1]^=p;
}
frep(i,N) {
frep(j,N) {
U[i][j]^=(U[i-1][j]^U[i][j-1]^U[i-1][j-1]);
cout << U[i][j];
if(j!=N)
cout << " ";
}
if(i!=N)
cout << "\n";
}
}


Second solution

n = int(raw_input())
m = int(raw_input())
grid = [[0 for __ in xrange(n+2)] for ___ in xrange(n+2)]
for i in range(m):
p,a,b,c,d = map(int, raw_input().split())
for i1 in [a-1,c]:
for i2 in [b-1,d]:
grid[i1][i2] ^= p

for i in range(n,-1,-1):
for j in range(n,-1,-1):
grid[i][j] ^= grid[i+1][j]^grid[i][j+1]^grid[i+1][j+1]

for i in range(1,n+1):
print " ".join(map(str,grid[i][1:n+1]))

Post a Comment

0 Comments