同http://www.cnblogs.com/ziyi--caolu/p/3202511.html
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int x1,y1,x2,y2; }s[1100]; int dp[1100],vist[1100][1100],n; int dfs(int num) { if(dp[num]>0) return dp[num]; dp[num]=1; for(int i=1;i<=n;i++) { if(vist[num][i]) { int tmp=dfs(i)+1; if(tmp>dp[num]) dp[num]=tmp; } } return dp[num]; } int main() { while(scanf("%d",&n)>0&&n) { for(int i=1;i<=n;i++) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); s[i].x1=x1; s[i].y1=y1; s[i].x2=x2; s[i].y2=y2; } memset(dp,0,sizeof(dp)); memset(vist,0,sizeof(vist)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(s[i].x2<s[j].x1&&s[i].y2<s[j].y1) vist[i][j]=1; } int maxx=0; for(int i=1;i<=n;i++) { int tmp=dfs(i); if(tmp>maxx) maxx=tmp; } printf("%d ",maxx); } return 0; }