cleaning robot

// Boss He translates

Problem Description

Boss Ho's company has produced a robot that can be used to clean up rubbish on sports fields after games. Before the robot started work, a grid-like map was given, and each square containing garbage was marked with a "G". All robots start out in the northwest corner, and at the end of the job all robots go to the southeast corner. Each robot can only walk in two directions, east and south. As soon as you reach the grid with garbage, the robot will clean up the garbage. Once the robot reaches the southeast corner, it automatically cuts off the power and cannot work again.
Your task is to figure out the minimum number of robots needed to clean up all the rubbish.
For example the picture below: All robots start at (1,1) and end at (6,7)
write picture description here
The picture below shows two possible solutions, the second one is better because it uses only two a robot.
write picture description here

input format

Consists of one or more lines, each line is an integer separated by two spaces, indicating the coordinates of a grid containing garbage. The
input is terminated by 0 0.
Note: The grid coordinates of each group of test data are given in ascending order of rows.
The number of rows and columns of the map does not exceed 24

output format

Several lines, each with an integer, represent the results corresponding to the test data.

sample input

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

Sample output

2


answer

Idea 1: DP

This problem can be transformed into finding the "longest ascending subsequence".
Obviously, if some of the garbage is arranged from the bottom left to the top right, any two of the garbage can't be collected by a robot. Therefore, the number of robots required is the length of the longest such sequence (obviously, other garbage can be cleaned by robots that clean these grids, otherwise the sequence can be lengthened).
Derived equations, available prefixes and optimizations.

Idea 2: Bipartite graph

Let's start by assuming that each square needs a robot.
Obviously, if a robot can go from garbage number i to garbage number j, then the robot in number j can not be used.
Moreover, after each robot has cleaned a certain garbage, it can only go to another grid, and each garbage can only be cleaned by one robot. This translates to a bipartite graph model (if garbage number i can reach garbage number j, connect the edge from i to j').
But this method is obviously not as good as DP in terms of space-time efficiency. . .


code

1、DP
#include <cstdio>
#include <iostream>
using namespace std;
int f[29][29],a[29][29];
int main()
{
    int i,j;
    while(true)
    {
        scanf("%d%d",&i,&j);
        if((!i)&&(!j))break;
        a[i][j]=1;
    }
    for(i=24;i;--i)
        for(j=1;j<=24;j++)
            f[i][j]=max(f[i+1][j],max(f[i][j-1],a[i][j]+f[i+1][j-1]));
    printf("%d",f[1][24]);
    return 0;
}
2. Bipartite graph
#include <cstdio>
#include <iostream>
using namespace std;
const int Q=1e3;
int link[Q],ch[Q],e[Q*Q],nn[Q*Q],last[Q],x[Q],y[Q],tot=0;
bool search(int x,int k)
{
    for(int t=last[x];t;t=nn[t])
    {
        int y=e[t];
        if(ch[y]==k)continue;
        ch[y]=k;
        if((!link[y])||search(link[y],k)){
            link[y]=x;return true;
        }
    }
    return false;
}
void add(int x,int y)
{
    e[++tot]=y;
    nn[tot]=last[x];
    last[x]=tot;
}
int main()
{
    int i,j,n=0,ans=0;
    while(true){
        scanf("%d%d",&x[n+1],&y[n+1]);
        if((!x[n+1])&&(!y[n+1]))break;
        ++n;
    }
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(i!=j&&x[i]<=x[j]&&y[i]<=y[j])
                add(i,j);
    for(i=1;i<=n;i++)
        if(search(i,i))ans++;
    printf("%d",n-ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325651910&siteId=291194637