Warm up 2
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 967 Accepted Submission(s): 465
Problem Description
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.
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
Source
Recommend
zhuyuanchen520
题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交。水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交。
二分匹配 最小点覆盖=最大匹配。
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m;
int g[1007][1007],link[1007];
bool vis[1007];
struct node
{
int x,y;
}h[1007],v[1007];
bool judge(int i,int j)
{
//注释掉的也能判断是否重合
/*if(h[i].x==v[j].x||h[i].x+1==v[j].x)
{
if(v[j].y==h[i].y||v[j].y+1==h[i].y)
return true;
}*/
//判断是否重合
if(h[i].x<=v[j].x&&v[j].x<=h[i].x+1)
{
if(v[j].y<=h[i].y&&h[i].y<=v[j].y+1)
return true;
}
return false;
}
bool find(int i)
{
for(int j=1;j<=m;j++)
if(g[i][j]&&!vis[j])
{
vis[j]=true;
if(link[j]==0||find(link[j]))
{
link[j]=i;
return true;
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
int count=0;
memset(g,0,sizeof(g));
memset(link,0,sizeof(link));
for(int i=1;i<=n;i++)
scanf("%d%d",&h[i].x,&h[i].y);
for(int i=1;i<=m;i++)
scanf("%d%d",&v[i].x,&v[i].y);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(judge(i,j))
{
g[i][j]=1;
}
for(int i=1;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))
count++;
}
printf("%d\n",n+m-count);
}
return 0;
}