题目

题意翻译
题意是给你一张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;
}

【CF37E】 Trial for Chief相关推荐

  1. 【POJ1113】Wall(凸包)

    [题目] Description Once upon a time there was a greedy King who ordered his chief Architect to build a ...

  2. 【 NLS 】Newton – Raphson Iteration Procedure of TOA - Based Positioning

    上篇博文: [ Notes ]NLS ALGORITHMS of TOA - Based Positioning说到了三种局部迭代算法进行TOA定位,分别为: Newton – Raphson Gau ...

  3. [转]机器学习和深度学习资料汇总【01】

    本文转自:http://blog.csdn.net/sinat_34707539/article/details/52105681 <Brief History of Machine Learn ...

  4. oracle11g知乎,【AAAI】AAAI2020录用论文汇总(二)

    因为AAAI的接受论文官方还没有放出,并且放的也是出奇的慢,本文汇总了23日在arxiv上挂出来的AAAI2020文章,供大家挑选感兴趣的文章下载.第一部分可以看 忆臻:[AAAI]AAAI2020录 ...

  5. 【caffe-Windows】以mnist为例的hdf5单标签处理——matlab实现

    前言 主要是想尝试看一下多标签的caffe是如何进行输入输出的,但是目前还未找到基于原始caffe做多标签输入的代码,大部分都是基于源码做了一部分修改实现多标签分类,caffe官网倒是有一个多标签的P ...

  6. linux下简单的备份的脚本 2 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4577034 之前写过 linux下简单 ...

  7. 【ISP】浅析Lens Shading

    [ISP]浅析Lens Shading 从2000年9月底摄像头首次出现在手机上算起,到如今成为诸多智能设备不可或缺的一部分,便携式手机摄像头已经走过了18年的发展历程.随着手机智能化.轻薄化的发展进 ...

  8. 【英语学习】【WOTD】hamartia 释义/词源/示例

    文章目录 Podcast hamartia *n.* [Hah-mahr-Tee-uh] Definition Did You Know? Examples Podcast hamartia podc ...

  9. 【Excel】可浮动利率(LPR)和提前还款的房贷计算器

    1 灵感来源 网上有现成的计算器,为什么还要自己做? 因为网页版的房贷计算器看不到过程,不知其所以然,不知道能不能完全信任. 加上朋友咨询提前还款的问题,又LPR开始执行,从现实和书里汲取了灵感,开始 ...

最新文章

  1. SAP MM 按采购订单查询付款信息的报表?
  2. ListFragment的使用
  3. 一个想法:走向“纳什均衡”是普遍存在的大趋势
  4. 【译】《Understanding ECMAScript6》- 第八章-Module
  5. 所谓高情商就是会说话--总结
  6. java怎么给list集合排序_java list集合排序按某一属性排序操作
  7. 字符串16进制之间相互转换(转载)
  8. 从零开始学前端:字体图标的引入 --- 今天你学习了吗?(CSS:Day18)
  9. python getattr_深入浅出Python模块
  10. 目标检测之Mtcnn网络详解(人脸检测)
  11. java 分隔函数split(,-1)的用途
  12. cad没有命令输入框_cad2017命令输入框没了
  13. 如何关闭笔记本电脑触摸板功能
  14. QT 调试时出现 Unable to create a debugging engine.
  15. 统信UOS命令大全 麒麟系统命令大全 Linux常用命令操作大全(非常全非常详细) ubuntu命令大全常用操作命令大全
  16. Nuget的使用说明
  17. vue中如何优雅实现爷孙组件的数据通信($attrs/$listeners)
  18. 干货来袭:抖音定位的重要性及抖音四步曲丨国仁网络资讯
  19. 周易Java_关于维度、计算机、周易的漫思
  20. 墨客科普 | MOAC BlockChain SafeMath库

热门文章

  1. 2023最新苹果APP上架App Store流程(超详细)
  2. android : 小米手机 打开开发者 选项 PC 端 安装 apk
  3. 微信服务号、订阅号和企业号的差别(运营和开发两个角度)
  4. 学习teardrop攻击并伪造一个ip包
  5. (压位)高精度乘法,高精度加法,高精度减法,高精度除法
  6. 【蓝桥杯】【Python】次数差
  7. 从软件保护到软件授权
  8. 如何做到四位验证码更简洁的生成?
  9. qq邮箱发送邮件到163邮箱
  10. 【GDOUCTF2023】wp