【Codeforces Beta Round #37】Codeforces 37E Trial for Chief

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

可以想到,每次染“一层”,“层数”就是答案。
具体实现上,枚举每个点求最短路【相同边权为0,不同边权为1】,他到最远的黑点的距离+1就是答案。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int oo=0x3f3f3f3f;
vector<pair<int,int> > to[3010];
deque<int> que;
int m,n,dis[3010],xx[]={0,0,1,-1},yy[]={1,-1,0,0};
bool vis[3010];
char a[60][60];
bool ok(int x,int y)
{
    return x>=1&&x<=n&&y>=1&&y<=m;
}
int solve(int x)
{
    int i,j,u,v,y,ret=-1;
    memset(vis,0,sizeof(vis));
    dis[x]=0;
    que.push_back(x);
    vis[x]=1;
    while (!que.empty())
    {
        u=que.front();
        que.pop_front();
        for (i=0;i<to[u].size();i++)
          if (!vis[v=to[u][i].first])
          {
            vis[v]=1;
            dis[v]=dis[u]+(y=to[u][i].second);
            if (y) que.push_back(v);
            else que.push_front(v);
          }
    }
    for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
        if (a[i][j]=='B')
          ret=max(ret,dis[i*m+j]);
    return ret+1;
}
int main()
{
    int i,j,k,x1,y1,ans=oo;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++)
      scanf("%s",a[i]+1);
    for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
        for (k=0;k<4;k++)
          if (ok(x1=i+xx[k],y1=j+yy[k]))
            to[i*m+j].push_back(make_pair(x1*m+y1,a[i][j]!=a[x1][y1]));
    for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
        ans=min(ans,solve(i*m+j));
    printf("%d\n",ans);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值