Shooting Targets
Coding Problem Keys
Shooting Targets
Problem Statement
N shooting targets are placed in a two dimensional grid and their x and y co-ordinates are provided. The x and y co-ordinates are integer value. When a bullet is fired along a straight line (either parallel to x-axis or y-axis) it eliminates all the targets along it's path.Assuming a bullet can be fired from negative infinity to positive infinity along a straight line parallel to both x and y-axis, find the minimum number of bullets that need to be fired to eliminate all targets.
Boundary Condition(s)
1 <= N <= 100-100 <= x, y <= 100
Input Format
The first line contains the value of N.The next N lines contain the two integers (x and y) in each line separated by space(s).
Output Format
The first line contains the minimum number of bullets that need to be fired to eliminate all targets.Example Input/Output 1
Input
94 0
4 1
3 0
2 1
1 1
-2 0
1 2
-1 -1
-2 -1
Output
4
Explanation
The first bullet is fired parallel to X-axis through the line Y = 0.
The second bullet is fired parallel to X-axis through the line Y = 1.
The third bullet is fired parallel to X-axis through the line Y = -1.
The fourth bullet is fired parallel to X-axis through the line Y = 2.
Example Input/Output 2
Input
82 3
-5 -1
0 4
1 2
7 2
4 0
3 5
1 1
Output
7Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: C Language
#include <stdio.h>
struct p{
int xy,c;
};
int f(int a,struct p *k,int l){
for(int i=0;i<l;i++){
if(k[i].xy==a)
return i;
}return -1;
}
void r(struct p *a,int x,int p[][2],int k,int l,int n){
for(int i=0;i<n;i++){
if(p[i][k]==x){
int h=f(p[i][k^1],a,l);
a[h].c--;
}
}
}
struct p * m(struct p *k,int l){
struct p *m=k;
for(int i=1;i<l;i++){
if(m->c<k[i].c)
m=k+i;
}return m;
}
int main(){
int n;
scanf("%d",&n);
int p[n][2],xc=0,yc=0,k,t=0,c=0;
struct p x1[n],y1[n],*tempx,*tempy;
for(int i=0;i<n;i++){
scanf("%d%d",&p[i][0],&p[i][1]);
k=f(p[i][0],x1,xc);
if(k==-1){
x1[xc].xy=p[i][0];
x1[xc++].c=1;
}else
x1[k].c++;
k=f(p[i][1],y1,yc);
if(k==-1){
y1[yc].xy=p[i][1];
y1[yc++].c=1;
}else
y1[k].c++;
}
tempy=m(y1,yc);
tempx=m(x1,xc);
while(t!=n){
if(tempx->c>tempy->c){
t+=tempx->c;
tempx->c=0;
r(y1,tempx->xy,p,0,yc,n);
tempx=m(x1,xc);
tempy=m(y1,yc);
}else{
t+=tempy->c;
tempy->c=0;
r(x1,tempy->xy,p,1,xc,n);
tempy=m(y1,yc);
tempx=m(x1,xc);
}c++;
}printf("%d",c);
}
// Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments