hdu 5319 Painter

来源:互联网 发布:windowsvps搭建数据库 编辑:程序博客网 时间:2024/06/12 01:08

题目描述:

有一个n × m的画板,画家在画板上斜着画了一些线,(即左上到右下’\’或右上到左下’/‘),这些线有两种颜色,红和蓝,斜向左下的只会是蓝色,斜向右下的只会是红色,每种颜色仅会染每个格子一次,如果一个格子同时染了红色和蓝色,那么它就会变成绿色。现在给出画板的最终状态,问画家最少画了几次线。

题解:

这道题目本身不难.如果每个方向染的颜色不同,那这题就变成了一个非常简单的贪心了。如果两个方向都可以染两种颜色,那么可以通过拆点之后求二分图最大匹配来解决这个问题。

我们重点看每次可以两种方向都染的情况: 本来是一个最小K覆盖. 但是有G颜色的就不好处理. 我们遇到这种情况一定要 拆 点 !!!因为混着看G点是不行的. 拆点之后就是正常的k覆盖. 并且还可以进一步看出:先对R点求k覆盖,再对B点求k覆盖就好了.

重点:

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <ctype.h>#include <limits.h>#include <cstdlib>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#include <set>#include <bitset>#define CLR(a) memset(a, 0, sizeof(a))#define REP(i, a, b) for(int i = a;i < b;i++)#define REP_D(i, a, b) for(int i = a;i <= b;i++)typedef long long ll;using namespace std;///max_v, V, get_G, 0 ~ V - 1;//v*E;const int MAXN = 1e4 + 100;const int INF = INT_MAX/2 - 1;vector<int> G[MAXN];int V;int m, n;int match[MAXN], used[MAXN];void add_edge(int from, int to){    G[from].push_back(to);    G[to].push_back(from);}int dfs(int v){    used[v] = 1;    for(int i = 0; i < G[v].size(); i++)    {        int u = G[v][i], w = match[u];        if(w < 0 || (!used[w] && dfs(w)))        {            match[u] = v;            match[v] = u;            return 1;        }    }    return 0;}int b_match()//最小k覆盖.{    int res = 0;    fill(match, match + V, -1);    for(int i = 0; i < V; i++)//其实到m就行了.    {        if(match[i] < 0)        {            CLR(used);            if(dfs(i))            {                res++;            }        }    }    return res;}char s[60][60];int check(int x, int y){    if(x >= 0 && x < n && y>=0 && y<m)        return 1;    return 0;}void solve(){    V = 2*n*m;    int sum = 0;    REP(i, 0, V)    {        G[i].clear();    }    REP(i, 0, n)    {        REP(j, 0, m)        {            if(s[i][j]=='G' || s[i][j]=='R')//只有一个方向可以建边.            {                sum++;                int newI = i-1, newJ = j-1;                if(check(newI, newJ) && (s[newI][newJ]=='G'||s[newI][newJ]=='R'))                {                    add_edge(newI*m+newJ, n*m+i*m+j);                }            }        }    }    int ans = sum - b_match();    sum = 0;    REP(i, 0, V)    {        G[i].clear();    }    REP(i, 0, n)    {        REP(j, 0, m)        {            if(s[i][j]=='G' || s[i][j]=='B')            {                sum++;                int newI = i-1;                int newJ = j + 1;                if(check(newI, newJ) && (s[newI][newJ]=='G'||s[newI][newJ]=='B'))                {                    add_edge(newI*m+newJ, n*m+i*m+j);                }            }        }    }    ans += sum-b_match();    printf("%d\n", ans);}int main(){    //freopen("13Min.txt", "r", stdin);    //freopen("1out.txt", "w", stdout);    int ncase;    scanf("%d", &ncase);    while(ncase--)    {        scanf("%d", &n);        REP(i, 0, n)        {            scanf("%s", s[i]);        }        m = strlen(s[0]);        solve();    }    return 0;}
0 0
原创粉丝点击