【CF37E】 Trial for Chief

题目

题意翻译
题意是给你一张N*M的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数;

题目描述
Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen.

As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of N N lines and M M columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chief’s slab is painted. The one who paints a slab like that first becomes the new chief.

Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way.

输入输出格式
输入格式:
The first line contains two integers N N and M M ( 1<=N,M<=50 1<=N,M<=50 ) — the number of lines and columns on the slab. The next N N lines contain M M symbols each — the final coloration of the slab. W W stands for the square that should be painted white and B B — for the square that should be painted black.

输出格式:
In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.

输入输出样例
输入样例#1:
3 3
WBW
BWB
WBW
输出样例#1:
2
输入样例#2:
2 3
BBB
BWB
输出样例#2:
1

思路

首先是结论:每个点向四个方向建边,如果颜色一样边权为0,否则为1,从每个点出发,跑一遍最短路,答案即为min(maxDistoBlackPoint)+1(特判全白)

证明可以这么想:

每经过一个不同的颜色,就要多染一次色,于是图上的点到最远的黑点的距离+1就是至少染色的次数;

染色这样,先染距离<=mx的,再染<=ans-1…<=0的,一共mx+1次;

为啥是黑色?因为初始是白的;

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<bitset>
#include<deque>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define ri register int
#define il inline
#define fi first
#define se second
#define mp make_pair
#define pi pair<int,int>
#define mem0(x) memset((x),0,sizeof (x))
#define mem1(x) memset((x),0x3f,sizeof (x))
#define gc getchar
template<class T>void in(T &x) {
    x = 0; bool f = 0; char c = gc();
    while (c < '0' || c > '9') {if (c == '-') f = 1; c = gc();}
    while ('0' <= c && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48); c = gc();}
    if (f) x = -x;
}
#undef gc
#define N 10000
#define M N<<3
int n, m;
bool hvb;
bool p[N][N];
int ans = inf;
int xx[] = { -1, 0, 1, 0}, yy[] = {0, 1, 0, -1};
il getn(int x, int y) {
    return m * (x - 1) + y;
}
int v[M], u[M], w[M], nx[M];
int cnt, head[N];
il void add(int uu, int vv, int ww) {
    u[++cnt] = uu, v[cnt] = vv, w[cnt] = ww, nx[cnt] = head[uu];
    head[uu] = cnt;
}
int d[N];
int vis[N];
void spfa(int s) {
    mem1(d);
    mem0(vis);
    queue<int>q;
    d[s] = 0;
    vis[s] = 1;
    q.push(s);
    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (ri i = head[x]; i; i = nx[i]) {
            if (d[v[i]] > d[x] + w[i]) {
                d[v[i]] = d[x] + w[i];
                if (!vis[v[i]]) {
                    vis[v[i]] = 1;
                    q.push(v[i]);
                }
            }
        }
        vis[x] = 0;
    }
}
vector<int> bl;
signed main() {
    in(n), in(m);
    char tc[3];
    for (ri i = 1; i <= n; ++i) {
        for (ri j = 1; j <= m; ++j) {
            scanf("%1s", tc);
            p[i][j] = (tc[0] == 'B');
            hvb |= p[i][j];
            if (p[i][j]) bl.push_back(getn(i, j));
        }
    }
    if (!hvb) {
        puts("0");
        return 0;
    }
    for (ri i = 1; i <= n; ++i) {
        for (ri j = 1; j <= m; ++j) {
            for (ri k = 0; k < 4; ++k) {
                int tx = i + xx[k], ty = j + yy[k];
                if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
                add(getn(i, j), getn(tx, ty), p[i][j] != p[tx][ty]);
            }
        }
    }
    for (ri i = 1; i <= n; ++i) {
        for (ri j = 1; j <= m; ++j) {
            int k = getn(i, j);
            spfa(k);
            int tmp = 0;
            for (ri h = 0, hi = bl.size(); h < hi; ++h) {
                tmp = max(tmp, d[bl[h]]);
            }
            ans = min(ans, tmp);
        }
    }
    printf("%d", ans + 1);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值