## 460 - Overlapping Rectangles

Moderator: Board moderators

Carunty
New poster
Posts: 18
Joined: Sat Jul 08, 2006 2:40 am

### 460 WA

I dont know why I get WA.
If you find any error in the output, please give me the sample input.

#include <iostream>
#include <string>
using namespace std;

int main(){
int p; cin >> p;
for(int i=0;i<p;++i){
if(i>0) cout << endl;
int a=0,b=0,c=9999,d=9999,w,x,y,z;
for(int j=0;j<2;++j){
bool m=true,n=true; cin>>w>>x>>y>>z;
if(a>=0){
if(a<=w&&w<=c){a=w; m=false;} if(b<=x&&x<=d) {b=x; n=false;}
if(a<=y&&y<=c){c=y; m=false;} if(b<=z&&z<=d) {d=z; n=false;}
}
if(m||n||a==c||b==d) a=-1;
}
if(a>=0) cout << a << " " << b << " " << c << " " << d << endl;
else cout << "No Overlap" << endl;
}
}

_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

### Try this.

Greetings!
Carunty, you are not considering the case where two rectangles form a cross. Try this input:

Code: Select all

``````1

2 0 4 6
0 2 6 4
``````
You should get this output:

Code: Select all

``````2 2 4 4
``````
Hope it helps!
Keep posting!
_.

rossi kamal
New poster
Posts: 14
Joined: Mon Sep 03, 2007 10:11 am
Contact:

### 460 WA ..can anyone help me on it?

//Rossi Kamal

#include<stdio.h>

int max(int xx,int yy);
int min(int aa,int bb);

int main()
{

//freopen("in460.txt","r",stdin);

int n;
while(scanf("%d",&n)!=EOF)
{
int i;
for(i=0;i<n;i++)
{
int l1,l2,d1,d2,r1,r2,u1,u2;
scanf("%d %d %d %d",&l1,&d1,&r1,&u1);
scanf("%d %d %d %d",&l2,&d2,&r2,&u2);

l1=max(l1,l2);
r1=min(r1,r2);
d1=max(d1,d2);
u1=min(u1,u2);

if(l1>=r1||d1>=u1)
printf("No Overlap\n");

else
printf("%d %d %d %d\n",l1,d1,r1,u1);

printf("\n");
}

}

return 0;
}

int max(int a,int b)
{
if(a>=b)
return a;
else
return b;
}

int min(int c,int d)
{
if(c<=d)
return c;
else
return d;
}

Plaguester
New poster
Posts: 1
Joined: Fri Oct 05, 2007 9:13 pm
This code works for every case that I have found for it, but it is getting WA. Any help would be appreciated.

Code: Select all

``code removed after AC``

amazingjxq
New poster
Posts: 2
Joined: Sat Oct 11, 2008 4:37 pm

### 460:I can't find what's wrong with my solution

Code: Select all

``````#include "stdio.h"
#include "math.h"
#include "iostream"
#include "algorithm"
using namespace std;

struct rect
{
int xll;
int xur;
int yll;
int yur;
};

struct rect overlap(struct rect r1,struct rect r2)
{
struct rect res={0,0,0,0};
float t1=fabs((float)(r1.xll+r1.xur)/2.0-(float)(r2.xll+r2.xur)/2.0),
t2=fabs((float)(r1.xll-r1.xur)/2.0)+fabs((float)(r2.xll-r2.xur)/2.0),
t3=fabs((float)(r1.yll+r1.yur)/2.0-(float)(r2.yll+r2.yur)/2.0),
t4=fabs((float)(r1.yll-r1.yur)/2.0)+fabs((float)(r2.yll-r2.yur)/2.0);
if(t1>=t2||t3>=t4) return res;
int x={r1.xll,r1.xur,r2.xll,r2.xur},y={r1.yll,r1.yur,r2.yll,r2.yur};
sort(x,x+4);
sort(y,y+4);
res.xll=x;res.xur=x;
res.yll=y;res.yur=y;
return res;
}

main()
{
struct rect r1,r2,res;
int n;
scanf("%d",&n);
while(n!=0){
scanf("%d%d%d%d%d%d%d%d",&r1.xll,&r1.yll,&r1.xur,&r1.yur,&r2.xll,&r2.yll,&r2.xur,&r2.yur);
res=overlap(r1,r2);
if(!res.xll&&!res.yll&&!res.xur&&!res.yur)printf("%s\n\n","No Overlap");
else cout<<res.xll<<" "<<res.yll<<" "<<res.xur<<" "<<res.yur<<" "<<endl<<endl;
n--;
}
}``````
First i judge whether the two window overlap,if so i give the 2nd and 3th of x as xll and xur,2nd and 3th of y as yll and yur,is there some problem with this?

amazingjxq
New poster
Posts: 2
Joined: Sat Oct 11, 2008 4:37 pm

### Re: 460:I can't find what's wrong with my solution

guys,i'm really confused. What's wrong?

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

### Re:  Is This Multiple Input??? -_-;;;

is my multiple input taking is ok or it is something different??
OR is there any other problem in the code?
here is the code:

Code: Select all

``````#include<stdio.h>

int max(int a,int b){
return (a>b)?a:b;
}

int min(int a,int b){
return (a<b)?a:b;
}

int main(){
long test,n,xll1,xll2,yll1,yll2,xur1,xur2,yur1,yur2;
//freopen("460.txt","r",stdin);
scanf("%ld",&test);
for(int it=0;it<test;it++){
scanf("%ld",&n);
for(int t=0;t<n;t++){
scanf("%ld %ld %ld %ld",&xll1,&yll1,&xur1,&yur1);
scanf("%ld %ld %ld %ld",&xll2,&yll2,&xur2,&yur2);
xll1=max(xll1,xll2);
yll1=max(yll1,yll2);
xur1=min(xur1,xur2);
yur1=min(yur1,yur2);
if((xll1>=xur1) || (yll1>=yur1)){
printf("No Overlap\n");
}
else{
printf("%ld %ld %ld %ld\n",xll1,yll1,xur1,yur1);
}
if(t<n-1)printf("\n");
}
if(it<test-1)printf("\n");
}
return 0;
}

``````
pls help i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

matrix
New poster
Posts: 3
Joined: Tue Aug 04, 2009 1:30 pm

### Re:  Is This Multiple Input??? -_-;;;

U know ur code is perfect
but hell ur input has a problem.
Just delete n bcos it never wants any input
of n;
if u solve then del it.

mfci
New poster
Posts: 1
Joined: Fri Dec 09, 2011 7:17 pm

### 460 - Overlapping Rectangles

Hello!
I can't find what's wrong with my solution...
Can you help me?

Here's my code! Thanks Code: Select all

``````package com;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RectangleOverlap extends JPanel{

int r1x1,r1x2,r2x1,r2x2 ;
int r1y1,r1y2,r2y1,r2y2 ;
int r1width,r2width ;
int r1height,r2height ;

static JButton btn = new JButton(“Check”);

public RectangleOverlap(int r1x1,int r1y1,int r1x2,int r1y2,int r2x1,int r2y1,int r2x2,int r2y2){

this.r1x1=r1x1;
this.r1x2=r1x2;
this.r1y1=r1y1;
this.r1y2=r1y2;
this.r2x1=r2x1;
this.r2x2=r2x2;
this.r2y1=r2y1;
this.r2y2=r2y2;

r1width = Math.abs(r1x1-r1x2);
r2width = Math.abs(r2x1-r2x2);
r1height = Math.abs(r1y1-r1y2);
r2height = Math.abs(r2y1-r2y2);

}

public void actionPerformed(ActionEvent e) {
checkOverlap();
}

});

}

private void checkOverlap() {

boolean isOVerlap= ((r1x2 >= r2x1) &&
(r1y2 >= r2y1) &&
(r1x1 <= r2x2) &&
(r1y1 <= r2y2));

if(isOVerlap ){

JOptionPane.showMessageDialog(null, “OVerlap”);

}else{

JOptionPane.showMessageDialog(null, “No OVerlap”);

}

}

@Override

protected void paintComponent(Graphics g) {

g.drawRect(r1x1,r1y1 , r1width, r1height);
g.setColor(new Color(123,232,122));
g.drawRect(r2x1, r2y1, r2width,r2height);

}

public static void main(String args[]){

JFrame frame = new JFrame();
frame.setSize(500,500);

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

frame.setVisible(true);

}
}
``````

biahll
New poster
Posts: 3
Joined: Mon Jun 25, 2012 11:10 pm

### Re: 460 - Overlapping Rectangles

Hey guys.

I'm getting WA with my code but it works for every input I've tested. Can anyone take a look at it?

Code: Select all

``````import java.io.BufferedReader;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
int XLL1, YLL1, XUR1, YUR1;
int XLL2, YLL2, XUR2, YUR2;
int XLLO, YLLO, XURO, YURO;
String parsedLine[];
String line;
for (int i = 0; i < t; i++) {
line = "";
while (line.trim().equals("")) {
}
parsedLine = line.split(" ");
XLL1 = Integer.parseInt(parsedLine);
YLL1 = Integer.parseInt(parsedLine);
XUR1 = Integer.parseInt(parsedLine);
YUR1 = Integer.parseInt(parsedLine);

XLL2 = Integer.parseInt(parsedLine);
YLL2 = Integer.parseInt(parsedLine);
XUR2 = Integer.parseInt(parsedLine);
YUR2 = Integer.parseInt(parsedLine);

XLLO = YLLO = XURO = YURO = 0;
XLLO = max(XLL1, XLL2);
YLLO = max(YLL1, YLL2);
XURO = min(XUR1, XUR2);
YURO = min(YUR1, YUR2);
if (XLLO < XURO && YLLO < YURO) {
System.out.println(XLLO + " " + YLLO + " " + XURO + " " + YURO);
System.out.println();
} else {
System.out.println("No Overlap");
System.out.println();
}
}

}

private static int max(int a, int b) {
if (a > b)
return a;
return b;
}

private static int min(int a, int b) {
if (a < b)
return a;
return b;
}
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 460 - Overlapping Rectangles

Don't print a blank line at the end of the output.
Check input and AC output for thousands of problems on uDebug!