Rectangles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 131 Accepted Submission(s): 53
Problem Description
A rectangle in the Cartesian plane is speci ed by a pair of coordinates (x1 , y1) and (x2 , y2) indicating its lower-left and upper-right corners, respectively (where x1 ≤ x2 and y1 ≤ y2). Given a pair of rectangles,A = ((x
A
1 , y
A
1 ), (x
A
2 ,y
A
2 )) and B = ((x
B
1 , y
B
1 ), (x
B
2 , y
B
2 )), we write A ≤ B (i.e., A "precedes" B), if x
A
2 < x
B
1 and y
A
2 < y
B
1 :In this problem, you are given a collection of rectangles located in the two-dimension Euclidean plane. Find the length L of the longest sequence of rectangles (A
1,A
2,…,A
L) from this collection such that A
1 ≤ A
2 ≤ … ≤ A
L.
Input
The input fi le will contain multiple test cases. Each test case will begin with a line containing a single integer n (where 1 ≤ n ≤ 1000), indicating the number of input rectangles. The next n lines each contain four integers x
i
1 ,y
i
1 ,x
i
2 ,y
i
2 (where -1000000 ≤ x
i
1 ≤ x
i
2 ≤ 1000000, -1000000 ≤ y
i
1 ≤ y
i
2 ≤ 1000000, and 1 ≤ i ≤ n), indicating the lower left and upper right corners of a rectangle. The end-of-file is denoted by asingle line containing the integer 0.
Output
For each input test case, print a single integer indicating the length of the longest chain.
Sample Input
3 1 5 2 8 3 -1 5 4 10 10 20 20 2 2 1 4 5 6 5 8 10 0
Sample Output
2 1#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
int maxn;
class rec
{
public:
int x1,y1,x2,y2;
};
vector <int > G[1005];
rec node[1005];
int in[1005],vis[1005],n;
int cnt[1005][1005];
void dfs(int step,int u)
{
if(cnt[step][u]==1) return ;
cnt[step][u]=1;
int flag=0;
for(int i=0;i<G[u].size();i++)
{
if(vis[G[u][i]]==0)
{
vis[G[u][i]]=1;
dfs(step+1,G[u][i]);
vis[G[u][i]]=0;
flag=1;
}
}
if(flag==0){if(step>maxn) maxn=step;return ;}
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
maxn=-1;
memset(cnt,-1,sizeof(cnt));
memset(in,0,sizeof(in));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=n;i++)scanf("%d%d%d%d",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(node[i].x2<node[j].x1&&node[i].y2<node[j].y1)
{
in[j]++;
G[i].push_back(j);
}
}
}
for(int i=1;i<=n;i++)
{
vis[i]=1;
if(in[i]==0) dfs(1,i);
vis[i]=0;
}
printf("%d/n",maxn);
}
return 0;
}