poj1548 Robots

http://www.elijahqi.win/2018/01/07/poj1548-robots/
Description
Your company provides robots that can be used to pick up litter from fields after sporting events and concerts. Before robots are assigned to a job, an aerial photograph of the field is marked with a grid. Each location in the grid that contains garbage is marked. All robots begin in the Northwest corner and end their movement in the Southeast corner. A robot can only move in two directions, either to the East or South. Upon entering a cell that contains garbage, the robot will pick it up before proceeding. Once a robot reaches its destination at the Southeast corner it cannot be repositioned or reused. Since your expenses are directly proportional to the number of robots used for a particular job, you are interested in finding the minimum number of robots that can clean a given field. For example, consider the field map shown in Figure 1 with rows and columns numbered as shown and garbage locations marked with a ‘G’. In this scheme, all robots will begin in location 1,1 and end in location 6, 7.

Figure 1 - A Field Map

Figure 2 below shows two possible solutions, the second of which is preferable since it uses two robots rather than three.

Figure 2 - Two Possible Solutions

Your task is to create a program that will determine the minimum number of robots needed to pick up all the garbage from a field.

Input
The input consists of one or more field maps followed by a line containing -1 -1 to signal the end of the input data. A field map consists of one or more lines, each containing one garbage location, followed by a line containing 0 0 to signal the end of the map. Each garbage location consists of two integers, the row and column, separated by a single space. The rows and columns are numbered as shown in Figure 1. The garbage locations will be given in row-major order. No single field map will have more than 24 rows and 24 columns. The sample input below shows an input file with two field maps. The first is the field map from Figure 1.

Output
The output will consist of a single line for each field map containing the minimum number of robots needed to clean the corresponding field.

Sample Input

1 2
1 4
2 4
2 6
4 4
4 7
6 6
0 0
1 1
2 2
4 4
0 0
-1 -1

Sample Output

2
1

Source
Mid-Central USA 2003
题意:给定一个矩阵 然后某些位置上有辣鸡每次只能从左上角出发 每次方向只能走两个方向右和下 求问最少需要多少个机器人可以把所有的辣鸡都捡走 那么根据Dilworth这个定理可以知道
对于偏序集X,
一个反链A是X的一个子集,它的任意两个元素都不能比较大小。
一个链C是X的一个子集,它的任意两个元素都可比。
这里可以比较的意思即为两个元素a,b之间有一个偏序的关系,具体参见离散数学。
有两个定理:
定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小。则X可以被划分成r个但不能再少的反链。
其对偶定理称为Dilworth定理:
定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小。则X可以被划分成m个但不能再少的链。
最少链划分数等于最长反链的长度 所以这个反链的长度可以二分图匹配去搞一搞的 因为最长反链的长度就是最小路径覆盖问题 那么直接匈牙利算法跑一下就okay 就是每个居于左上的点都可以去连接右下的点 用总数减一下最大匹配即可
此题还可以dp搞一搞
匈牙利:

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 600
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}return x*f;
}
struct node{
    int x,y,next;
}data[200000];
struct pt{
    int x,y;
}point[N];
int num,h[N],gf[N],used[N];
inline void insert1(int x,int y){
    data[++num].y=y;data[num].next=h[x];h[x]=num;data[num].x=x;
}
inline int find(int x){
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if(used[y]) continue;used[y]=1;
        if(!gf[y]||find(gf[y])){
            gf[y]=x;return 1;
        }
    }return 0;
}
int main(){
    freopen("poj1548.in","r",stdin);
    while(1){
        bool flag=0;memset(h,0,sizeof(h));int tot=0;memset(gf,0,sizeof(gf));num=0;
        while(1){
            point[++tot].x=read();point[tot].y=read();
            if (!point[tot].x&&!point[tot].y) break;
            if (point[tot].x==-1&&point[tot].y==-1){flag=1;break;}
        }--tot;if (flag) break;
        for (int i=1;i<=tot;++i)
            for (int j=i+1;j<=tot;++j){
                if(point[i].x>point[j].x||point[i].y>point[j].y) continue;insert1(i,j);
            }
        int ans=0;
        for (int i=1;i<=tot;++i){
            memset(used,0,sizeof(used));
            if(find(i))++ans;
        }printf("%d\n",tot-ans);
    }
    return 0;
}

dp的做法

帮我再次加深理解了下这题的做法


#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 600
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}return x*f;
}
struct pt{
    int x,y;
}point[N];
int dp[N];
inline bool cmp(pt a,pt b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int main(){
    freopen("poj1548.in","r",stdin);
    while(1){
        bool flag=0;int tot=0;memset(dp,0,sizeof(dp));
        while(1){
            point[++tot].x=read();point[tot].y=read();
            if (!point[tot].x&&!point[tot].y) break;
            if (point[tot].x==-1&&point[tot].y==-1){flag=1;break;}
        }--tot;if (flag) break;sort(point+1,point+tot+1,cmp);int ans=0;
        for (int i=1;i<=tot;++i){
            dp[i]=1;
            for (int j=1;j<i;++j) 
                if (point[j].y>point[i].y&&dp[i]<dp[j]+1) dp[i]=dp[j]+1;
            if (dp[i]>ans) ans=dp[i];
        }printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值