HDU--3457--Rectangles--记忆化搜索

Rectangles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 707    Accepted Submission(s): 284 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 = ((xA1 , yA1 ), (xA2 ,yA2 )) and B = ((xB1 , yB1 ), (xB2 , yB2 )), we write A ≤ B (i.e., A "precedes" B), if xA2 < xB1 and yA2 < yB1 :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 (A1,A2,…,AL) from this collection such that A1 ≤ A2 ≤ … ≤ AL.   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 xi1 ,yi1 ,xi2 ,yi2 (where -1000000 ≤ xi1 ≤ xi2 ≤ 1000000, -1000000 ≤ yi1 ≤ yi2 ≤ 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

题意:给你N个矩形的左下角跟右上角坐标,然后找出这样一组序列使得其中的每一项与它的后一项满足一个条件,条件是前一项的右上角顶点的X,Y坐标都比后一项的左下角顶点的小,这样的序列可能有多个,求出最长那个的长度(最后一项没有后一项,你懂的,他就是最大的那个)

解析:用记忆化搜索来减少重复搜索的时间,做了很多记忆化搜索题目的人基本很快KO这个题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max(a,b) a>b?a:b
using namespace std;
int n;
int dp[1111],map[1111][1111];
struct node
{
    int x1,x2,y1,y2;
}s[1111];
int dfs(int x)
{
    int i,j,k,l;
    if(dp[x])return dp[x];	//先前有数据了就不用再搜了
    for(i=0;i<n;i++)
    if(map[x][i])
    {
        dp[x]=Max(dp[x],dfs(i)+1);	//dp[x]要记录的是最大的那个值,值:由这个点往后最长的序列长度
    }
    dp[x]=Max(dp[x],1);	//当然如果是最大的那个就要赋值1
    return dp[x];
}
int main (void)
{
    int i,j,k,l,x1,x2,y1,y2,sum;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            s[i].x1=x1;
            s[i].x2=x2;
            s[i].y1=y1;
            s[i].y2=y2;
        }
        memset(map,0,sizeof(map));
        memset(dp,0,sizeof(dp));
        for(i=0;i<n;i++)	//建立关联方便搜索
        for(j=0;j<n;j++)
        {
            if(s[i].x2<s[j].x1&&s[i].y2<s[j].y1)
            {
                map[i][j]=1;
            }
        }
        sum=0;
        for(i=0;i<n;i++)	//可能最长的那个序列的第一个矩形不是输入序列中的第一个
        {
            sum=Max(sum,dfs(i));
        }
        printf("%d\n",sum);
    }
    return 0;
}

总结:记录最优状态,以后搜索到这里的时候就可以直接调用这个状态

时间: 2024-10-13 07:53:04

HDU--3457--Rectangles--记忆化搜索的相关文章

HDU 4597(记忆化搜索 dfs 参考)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from

hdu 1978(记忆化搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2945    Accepted Submission(s): 1727 Problem Description 这是一个简单的生存游戏,你控制一个机器人从一

不要62 hdu 2089 dfs记忆化搜索

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中的 dfs 记忆化搜索方法解. 模板: int dfs(int i, int s, bool e) { if (i==-1) return s==target_s; if (!e && f[i][s] != -1) return f[i][s]; int res = 0; int u = e?

POJ 2192 &amp;&amp; HDU 1501 Zipper (记忆化搜索)

Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16803   Accepted: 5994 Description Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first tw

hdu 1501 Zipper 记忆化搜索

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7188    Accepted Submission(s): 2571 Problem Description Given three strings, you are to determine whether the third string can be formed

poj 1198 / hdu 1401 Solitaire (记忆化搜索+meet in middle)

题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的棋子由初始排布变成目标排布 8*8的棋盘,刚好不爆ull,状压那些位置有棋子 然后从初始状态开始,暴搜出当前状态下,移动一个棋子之后所有可能到达的状态 直接搜,总状态数是8^8,此外还有常数,会爆 由于给定了目标排布,考虑meet in middle 从起始状态和目标状态各搜4步即可 为了防止爆栈,

HDU 1242 Rescus(记忆化搜索)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu1078 记忆化搜索

/* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,l; int m[105][105]; int dp[105][105]; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1};

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-

HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6350    Accepted Submission(s): 2332 Problem Description Jimmy experiences a lot of stress at work these days, especial