当前位置 : 主页 > 编程语言 > java >

Oil Skimming (二分图最大匹配)

来源:互联网 收集:自由互联 发布时间:2022-08-15
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 bar


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;
}




上一篇:「应用架构」TOGAF建模:流程/系统实现图
下一篇:没有了
网友评论