Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Description

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 lines and 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.

Input

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

Output

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

Sample Input

Input
3 3
WBW
BWB
WBW
Output
2
Input
2 3
BBB
BWB
Output
1

Source

逆向思维,从目标图开始将图染成初始图。每染一次色,联通块就会扩大,(类似colorflood)。

那么如何计算代价?

从每个点向四周连边,同色代价为0,异色代价为1,O(n^2)枚举起点,跑SPFA,看何时“最远代价最小”

↑注意特判:如果终态染成了全黑的图,因为初始图是全白,所以代价+1

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define LL long long
using namespace std;
const int mx[]={,,,-,};
const int my[]={,,,,-};
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
int dis;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int d){
e[++mct].v=v;e[mct].dis=d;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int n,m;
char mp[][];
int id[][];
bool inq[mxn];
int dis[mxn];
int SPFA(int s){
memset(dis,0x3f,sizeof dis);
queue<int>q;
q.push(s);
inq[s]=;
dis[s]=;
while(!q.empty()){
int u=q.front();q.pop();inq[u]=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]>dis[u]+e[i].dis){
dis[v]=dis[u]+e[i].dis;
if(!inq[v]){
inq[v]=;
q.push(v);
}
}
}
}
int res=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(mp[i][j]=='W')res=max(res,dis[id[i][j]]);
else res=max(res,dis[id[i][j]]+);
return res;
}
int main()
{
n=read();m=read();
int i,j;
for(i=;i<=n;i++)
scanf("%s",mp[i]+);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
id[i][j]=(i-)*m+j;
for(i=;i<=n;i++)
for(j=;j<=m;j++){
for(int k=;k<=;k++){
int nx=i+mx[k];
int ny=j+my[k];
if(nx< || nx>n || ny< || ny>m)continue;
if(mp[i][j]==mp[nx][ny]){
add_edge(id[i][j],id[nx][ny],);
add_edge(id[nx][ny],id[i][j],);
}
else{
add_edge(id[i][j],id[nx][ny],);
add_edge(id[nx][ny],id[i][j],);
}
}
}
int ans=1e9;
for(i=;i<=n;i++)
for(j=;j<=m;j++){
ans=min(ans,SPFA(id[i][j]));
}
printf("%d\n",ans);
return ;
}

CodeForces 37E Trial for Chief的更多相关文章

  1. codeforces 37 E. Trial for Chief【spfa】

    想象成一层一层的染,所以相邻的两个格子连边,边权同色为0异色为1,然后答案就是某个格子到距离它最远得黑格子的最短距离的最小值 注意特判掉不需要染色的情况 #include<iostream> ...

  2. [CF] 37 E. Trial for Chief

    如果固定了一个中心,那么只需要考虑从它开始最远染到的那些点究竟染了几次. 上下左右不同的点连1边,相同的连0边,跑单源最短路就可以啦. lyd讲的是统计到最远黑点+1的最小值,但是#58数据全是白点, ...

  3. CF37E Trial for Chief(最短路)

    题意 题意是给你一张 NMNMNM 的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数:(n,m<=50) 题解 转化为最短路.对于每一个点与它相邻的 ...

  4. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

  5. NOIP前的水题记录

    CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...

  6. Educational Codeforces Round 37-E.Connected Components?题解

    一.题目 二.题目链接 http://codeforces.com/contest/920/problem/E 三.题意 给定一个$N$和$M$.$N$表示有$N$个点,$M$表示,在一个$N$个点组 ...

  7. Codeforces Gym 100513G G. FacePalm Accounting

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  8. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  9. Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题

    D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...

随机推荐

  1. VMware Fusion DHCP方式下如何指定虚拟机IP地址

    默认情况下,vmware fusion中的虚拟机,网卡设置成dhcp(动态分配 )时,会分配一个IP地址,但这个IP通常很难记,如果我们想为某台虚拟机挑一个好记的IP地址,可以按如下步骤操作: 命令行 ...

  2. jboss的时区问题

    默认情况下,jboss启动时,使用的时区是“+0:00”区,而中国所在的时间为"+8:00"区(所谓的东8区),最终java取当前时间时,总比北京时间慢8个小时 解决办法: 新建一 ...

  3. 软件工程(DBSD2016) Git Review

    说明:任何问题请在评论区说明,会集中更新回复. 连连看组 源码: git clone https://git.coding.net/jx8zjs/llk.git 提交日志 一共有20次commit日志 ...

  4. 机械大楼电梯控制项目软件 -- github团队组建

    目前在Github网站上建立了机械大楼电梯控制项目软件的软件仓库(Repository),提供了软件功能需求说明文档和Automation Studio程序模板.地址为 https://github. ...

  5. OS存储器管理(三) 虚拟存储器

    基本概念与实现 1)局部性原理 在一段时间内,运行的作业程序仅访问(涉及到)一部分作业代码,即不会涉及整个地址空间.即在一段时间间隔内,仅装入一部分代码,作业照样能正常运行 2)虚拟存储器的引入 作业 ...

  6. 又发现一个msdn的坑

    一个类型里面有两个属性仅仅是大小写区别,可是IIS不区分大小写,问:如何才能查看两个属性里面的文档那? http://msdn.microsoft.com/en-us/library/microsof ...

  7. 生成短链(网址) ShortUrlLink

    建表 CREATE TABLE [dbo].[ShortUrl]( [Id] [,) NOT NULL, [LongUrl] [nvarchar]() NOT NULL, [BaseUri] [int ...

  8. [转]java 输出流转输入流

    ByteArrayOutputStream.toByteArray ByteArrayInputStream StringWriter.toString StringReader 字符流和二进制流是j ...

  9. Beta版本冲刺Day4

    会议讨论: 628:由于昨天的考试我们组目前只把项目放到了服务器上,配置Java环境遇到了问题.601:将一些原来的界面进行了修改,修改成了更加美观的外形. 528:进行一些还未完成得到功能,比如查询 ...

  10. 1020理解MySQL——索引与优化

    转自http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性 ...