码迷,mamicode.com
首页 > 其他好文 > 详细

J - Oil Skimming 二分图的最大匹配

时间:2014-08-19 10:32:05      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   for   

Description

bubuko.com,布布扣

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.

Input

The input starts with an integer K ( 1bubuko.com,布布扣Kbubuko.com,布布扣100) indicating the number of cases. Each case starts with an integer N ( 1bubuko.com,布布扣Nbubuko.com,布布扣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.

Output

For 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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

#define N 1210
int cx[N];
int cy[N];
int nx,ny;
int mk[N];
vector<int> map[N];
int ma[N][N];
char g[610][610];

int path(int u)
{
    int len = map[u].size();
    for(int i = 0; i < len; i ++)
    {
        int v = map[u][i];
        if(!mk[v])
        {
            mk[v] = 1;
            if(cy[v] == -1 || path(cy[v]))  ///cy #号块也没有动||cy 也有符合条件的
            {
                cx[u] = v;
                cy[v] = u;
                return 1;
            }

        }
    }
    return 0;
}
int maxma()
{
    int res = 0;
    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));
    for(int i = 0; i < nx; i ++)
    {
        if(cx[i] == -1)   ///#号块儿 没动
        {
            memset(mk,0,sizeof(mk));
            res += path(i);
            //printf("%d---\n",res);
        }
    }
    return res;
}
int main()
{
    int t,n;memset(g,0,sizeof(g));
    //freopen("a.txt","r",stdin);
    scanf("%d",&t);
    int ca = 1;
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 1;i <= n*n;i ++)
           map[i].clear();  ///初始化
        int num = 0;
        //memset(map,0,sizeof(map));
        for(int i = 1; i <= n; i ++)
        {
            scanf("%s",g[i]+1);
            for(int j = 1;j <= n;j ++)
               if(g[i][j]==#) ma[i][j] = num++;  ///多少#
            //printf("||%s\n",g[i]+1);
        }
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)   ///符合条件的
            {
                if(g[i][j] != #) continue;
                if(g[i][j] == # && # == g[i+1][j])
                    map[ma[i][j]].push_back(ma[i+1][j]);
                if(g[i][j] == # && g[i-1][j] == #)
                    map[ma[i][j]].push_back(ma[i-1][j]);
                if(g[i][j] == # && g[i][j+1] == #)
                    map[ma[i][j]].push_back(ma[i][j+1]);
                if(g[i][j] == # && g[i][j-1] == #)
                    map[ma[i][j]].push_back(ma[i][j-1]);
            }
        nx = ny = num;
        printf("Case %d: %d\n",ca++,maxma()/2);
    }
    return 0;
}

 

J - Oil Skimming 二分图的最大匹配,布布扣,bubuko.com

J - Oil Skimming 二分图的最大匹配

标签:des   style   blog   http   color   os   io   for   

原文地址:http://www.cnblogs.com/zhangying/p/3921260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!