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. Caffe学习系列(2):数据层及参数

    要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等, 而一个模型由多个屋(layer)构成,每一屋又由许多参数组成.所有的参数都定义在caffe.proto这个文件 ...

  2. Java7并发编程实战(一) 线程的中断

    控制线程中断的方法一般常规是定义一个布尔值,然后while(布尔值) 去执行,当想停止该线程时候,把布尔值设为false. 这里我们来看第二种,Interrupt 该例子模拟一个线程从1打印到10,然 ...

  3. mvc5 Html.EditorFor html属性有了新变化,和以前的不同了

    @Html.EditorFor(model => model.MaxNumber, new { htmlAttributes = new { @min = "1" } })

  4. [转]关于Python中的yield

    在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...

  5. CUDA1.1-函数类型限定符与变量类型限定符

    这部分来自于<CUDA_C_Programming_Guide.pdf>,看完<GPU高性能变成CUDA实战>的第四章,觉得这本书还是很好的,是一种循序渐进式的书,值得看,而不 ...

  6. WPF 小技巧

    在使用mvvm模式开发时,对于Command的绑定是一件很伤脑筋的事情,尽管有强大的Blend类库支持: xmlns:Custom="http://www.galasoft.ch/mvvml ...

  7. Chrome 监听 console 打开

    这个算是 Chrome only 其他的我没测试,也不想测试.因为我的控制台脚本仅仅在 Chrome 下加载. 如果你需要全平台,那么这肯定不是你需要的结果. 需求 其实我很早就想折腾这个了,但是,, ...

  8. 一起来学node.js吧 node school简介

    node.js这几年火爆的简直丧心病狂,去lagou.com查查node.js的职位,那叫一个多. 要说火爆到什么程度,竟然有一个网站专门去教大家学习node.js, Node School. 进去逛 ...

  9. Xamarin Android -创建Splash Screen (一)

    ......(空话少说) Xamarin 开发的技术资料很少,通过学习,把自己的学习过程及其中碰到的问题与大家分享. Splash Screen 就是在应用程序启动时,出现的一张图片,一般App的Sp ...

  10. GBPR: Group Preference Based Bayesian Personalized Ranking for One-Class Collaborative Filtering-IJACA 2013_20160421

    1.Information publication:IJACA 2013 2.What 基于BPR模型的改进:改变BPR模型中,a,用户对商品喜好偏序对之间相互独立;b,用户之间相互独立的假设 原因: ...