CodeForces 37E Trial for Chief

时间:2022-06-10 00:58:15
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 ;
}