HDU - 4619 I - Warm up 2

Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
Input
  There are multiple input cases.
  The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
  Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
  Input ends with n = 0 and m = 0.
Output
  For each test case, output the maximum number of remaining dominoes in a line.
Sample Input
2 3
0 0
0 3
0 1
1 1
1 3
4 5
0 1
0 2
3 1
2 2
0 0
1 0
2 0
4 1
3 2
0 0
Sample Output
4

6

题意:多米诺骨牌,简化题目就是给你一些水平的线和一些竖直的线,问让你去掉几条边可以使所有的边不想交(线是由一个点给的另一点是水平下一个点或者是竖直下一个点),

题解:比赛的时候谢了暴力搜索的,但是却一直没交过,赛后看题解是二分图匹配的最大匹配问题就学了一下匈牙利算法,这个题给出的问题正好符合二分图的条件,去掉多少条边可以使所有的边不想交,正好对应最小覆盖点,而最小覆盖点=最大匹配数,所以直接用匈牙利算法解决就可以了

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int map[1005][1005],phi[1005][1005],cx[1005],cy[1005],n,m,vis[1005];
bool path(int v)
{
    for(int i=0;i<m;i++)
    {
        if(phi[v][i]&&vis[i]==0)
        {
            vis[i]=1;
            if(cy[i]==-1||path(cy[i]))
            {
                cx[v]=i;
                cy[i]=v;
                return true;
            }
        }
    }
    return false;
}
int maxMatch()
{
   int ans=0;
   memset(cx,-1,sizeof(cx));
   memset(cy,-1,sizeof(cy));
   for(int i=0;i<n;i++)
   {
       if(cx[i]==-1)
       {
         memset(vis,0,sizeof(vis));
         if(path(i))
          ans++;
       }
   }
   return ans;
}
int main()
{
    int x,y;
    while(cin>>n>>m&&n!=0)
    {
        memset(map,-1,sizeof(map));
        for(int i=0;i<n;i++)
        {
          scanf("%d%d",&x,&y);
          map[x][y]=i;
          map[x+1][y]=i;
        }
        memset(phi,0,sizeof(phi));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            if(map[x][y]!=-1)
            phi[map[x][y]][i]=1;
            if(map[x][y+1]!=-1)
            phi[map[x][y+1]][i]=1;
        }
        cout<<n+m-maxMatch()<<endl;
    }
    return 0;
}

但赛后还看了牟大神的代码,发现他就是暴力搜索写过的,下面是暴力搜索的代码:

#include <iostream>
#include <cstring>

using namespace std;

int x1,y1,num,t;
int vis[105][105];
int xvis[105][105];
int yvis[105][105];
int r[4]={0,0,1,-1};
int c[4]={1,-1,0,0};

void dfs(int x,int y)
{
    num++;
    vis[x][y]=0;
    if(xvis[x][y]==1)
        t=2;
    else
        t=3;
    x1=x+r[t];
    y1=y+c[t];
    if(vis[x1][y1]==2)
        dfs(x1,y1);
    if(yvis[x][y]==1)
        t=0;
    else
        t=1;
    x1=x+r[t];
    y1=y+c[t];
    if(vis[x1][y1]==2)
        dfs(x1,y1);
}

int main()
{
    int u,v,m,n,ans;
    while(cin>>m>>n&&m)
    {
        ans=m+n;
        memset(vis,0,sizeof(vis));
        memset(xvis,0,sizeof(xvis));
        memset(yvis,0,sizeof(yvis));
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v;
            vis[u][v]+=1;
            vis[u+1][v]+=1;
            xvis[u][v]=1;
            xvis[u+1][v]=2;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>u>>v;
            vis[u][v]+=1;
            vis[u][v+1]+=1;
            yvis[u][v]=1;
            yvis[u][v+1]=2;
        }
        for(int i=0;i<=101;i++)
        {
            for(int j=0;j<=101;j++)
            {
                if(vis[i][j]==2)
                {
                    num=0;
                    dfs(i,j);
                    if(num%2==0)
                    ans-=num/2;
                    else
                    ans-=(num+1)/2;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值