如何通过油面撇除技术实现二分图最大匹配?
- 内容介绍
- 文章标签
- 相关推荐
本文共计998个文字,预计阅读时间需要4分钟。
感谢一家特定的绿色资源公司,一个新的油污清除产业应运而生。墨西哥湾漂浮着大量的原油,正等待着有远见的石油大亨将其捞取。一位这样的石油大亨说道:
Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
InputThe input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.OutputFor each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.Sample Input
1 6 ...... .##... .##... ....#. ....## ......
Sample Output
Case 1: 3
题目大概:
在一个n*n的矩形中有很多油,一次可以取1*2矩形内的油(且矩形内必须都是油),问最多取几次。
思路:
这个虽然也是矩形问题,矩形中也有东西需要操作,但是却不是用之前的行列为点,焦点为边的建模法。而是,把所有有油的地方标记下来并且重新编号,然后扫一遍矩形找和它相邻的点有油的地方连边,最后跑一遍匈牙利算法,求最大匹配,因为连的是双向边,a扫到b要取,b扫到a也要取,所以答案要/2.。
代码:
#include <cstdio>#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 660
using namespace std;
char map[MAXN][MAXN];
int ma[MAXN][MAXN];
int G[MAXN][MAXN];
int pipei[MAXN];
bool used[MAXN];
int N,T,M;
void init()
{
memset(G,0,sizeof(G));
}
void so(int i,int j)
{
if(ma[i][j+1]&&j+1<N)
{
G[ma[i][j]][ma[i][j+1]]=G[ma[i][j+1]][ma[i][j]]=1;
//printf("%d %d\n",ma[i][j],ma[i][j+1]);
}
if(ma[i+1][j]&&i+1<N)
{
G[ma[i][j]][ma[i+1][j]]=G[ma[i+1][j]][ma[i][j]]=1;
//printf("%d %d\n",ma[i][j],ma[i+1][j]);
}
}
void getMap()
{
M=1;
for(int i = 0; i < N; i++)
{
scanf("%s",map[i]);
for(int j=0;j<N;j++)
{
if(map[i][j]=='#')
{
ma[i][j]=M++;
}
else
{
ma[i][j]=0;
}
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(ma[i][j])
{
//printf("%d \n",ma[i][j]);
so(i,j);
}
}
}
}
int find(int x)
{
for(int i = 1; i <M; i++)
{
int y = G[x][i];
if(y&&!used[i])
{
used[i] = true;
if(pipei[i] == -1 || find(pipei[i]))
{
pipei[i] = x;
return 1;
}
}
}
return 0;
}
int te=0;
void solve()
{
/*printf("%d\n",M);
for(int i=1;i<M;i++)
{
for(int j=1;j<M;j++)
{
printf("%d ",G[i][j]);
}
printf("\n");
}*/
int ans = 0;
memset(pipei, -1, sizeof(pipei));
for(int i = 1; i <M; i++)
{
memset(used, false, sizeof(used));
ans += find(i);
}
printf("Case %d: %d\n",te,ans/2);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d", &N);
te++;
init();
getMap();
solve();
}
return 0;
}
本文共计998个文字,预计阅读时间需要4分钟。
感谢一家特定的绿色资源公司,一个新的油污清除产业应运而生。墨西哥湾漂浮着大量的原油,正等待着有远见的石油大亨将其捞取。一位这样的石油大亨说道:
Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
InputThe input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.OutputFor each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.Sample Input
1 6 ...... .##... .##... ....#. ....## ......
Sample Output
Case 1: 3
题目大概:
在一个n*n的矩形中有很多油,一次可以取1*2矩形内的油(且矩形内必须都是油),问最多取几次。
思路:
这个虽然也是矩形问题,矩形中也有东西需要操作,但是却不是用之前的行列为点,焦点为边的建模法。而是,把所有有油的地方标记下来并且重新编号,然后扫一遍矩形找和它相邻的点有油的地方连边,最后跑一遍匈牙利算法,求最大匹配,因为连的是双向边,a扫到b要取,b扫到a也要取,所以答案要/2.。
代码:
#include <cstdio>#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 660
using namespace std;
char map[MAXN][MAXN];
int ma[MAXN][MAXN];
int G[MAXN][MAXN];
int pipei[MAXN];
bool used[MAXN];
int N,T,M;
void init()
{
memset(G,0,sizeof(G));
}
void so(int i,int j)
{
if(ma[i][j+1]&&j+1<N)
{
G[ma[i][j]][ma[i][j+1]]=G[ma[i][j+1]][ma[i][j]]=1;
//printf("%d %d\n",ma[i][j],ma[i][j+1]);
}
if(ma[i+1][j]&&i+1<N)
{
G[ma[i][j]][ma[i+1][j]]=G[ma[i+1][j]][ma[i][j]]=1;
//printf("%d %d\n",ma[i][j],ma[i+1][j]);
}
}
void getMap()
{
M=1;
for(int i = 0; i < N; i++)
{
scanf("%s",map[i]);
for(int j=0;j<N;j++)
{
if(map[i][j]=='#')
{
ma[i][j]=M++;
}
else
{
ma[i][j]=0;
}
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(ma[i][j])
{
//printf("%d \n",ma[i][j]);
so(i,j);
}
}
}
}
int find(int x)
{
for(int i = 1; i <M; i++)
{
int y = G[x][i];
if(y&&!used[i])
{
used[i] = true;
if(pipei[i] == -1 || find(pipei[i]))
{
pipei[i] = x;
return 1;
}
}
}
return 0;
}
int te=0;
void solve()
{
/*printf("%d\n",M);
for(int i=1;i<M;i++)
{
for(int j=1;j<M;j++)
{
printf("%d ",G[i][j]);
}
printf("\n");
}*/
int ans = 0;
memset(pipei, -1, sizeof(pipei));
for(int i = 1; i <M; i++)
{
memset(used, false, sizeof(used));
ans += find(i);
}
printf("Case %d: %d\n",te,ans/2);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d", &N);
te++;
init();
getMap();
solve();
}
return 0;
}

