CodeForces 37E Trial for Chief

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 3WBWBWBWBW

Output

2

Input

2 3BBBBWB

Output

1

Source

Codeforces Beta Round #37

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

那么如何计算代价?

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

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 #define LL long long
 7 using namespace std;
 8 const int mx[5]={0,1,0,-1,0};
 9 const int my[5]={0,0,1,0,-1};
10 const int mxn=18510;
11 int read(){
12     int x=0,f=1;char ch=getchar();
13     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
14     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
15     return x*f;
16 }
17 struct edge{
18     int v,nxt;
19     int dis;
20 }e[mxn<<1];
21 int hd[mxn],mct=0;
22 void add_edge(int u,int v,int d){
23     e[++mct].v=v;e[mct].dis=d;e[mct].nxt=hd[u];hd[u]=mct;return;
24 }
25 int n,m;
26 char mp[60][60];
27 int id[60][60];
28 bool inq[mxn];
29 int dis[mxn];
30 int SPFA(int s){
31     memset(dis,0x3f,sizeof dis);
32     queue<int>q;
33     q.push(s);
34     inq[s]=1;
35     dis[s]=0;
36     while(!q.empty()){
37         int u=q.front();q.pop();inq[u]=0;
38         for(int i=hd[u];i;i=e[i].nxt){
39             int v=e[i].v;
40             if(dis[v]>dis[u]+e[i].dis){
41                 dis[v]=dis[u]+e[i].dis;
42                 if(!inq[v]){
43                     inq[v]=1;
44                     q.push(v);
45                 }
46             }
47         }
48     }
49     int res=0;
50     for(int i=1;i<=n;i++)
51      for(int j=1;j<=m;j++)
52          if(mp[i][j]==‘W‘)res=max(res,dis[id[i][j]]);
53          else res=max(res,dis[id[i][j]]+1);
54     return res;
55 }
56 int main()
57 {
58     n=read();m=read();
59     int i,j;
60     for(i=1;i<=n;i++)
61         scanf("%s",mp[i]+1);
62     for(i=1;i<=n;i++)
63      for(j=1;j<=m;j++)
64          id[i][j]=(i-1)*m+j;
65     for(i=1;i<=n;i++)
66      for(j=1;j<=m;j++){
67          for(int k=1;k<=4;k++){
68              int nx=i+mx[k];
69              int ny=j+my[k];
70              if(nx<1 || nx>n || ny<1 || ny>m)continue;
71              if(mp[i][j]==mp[nx][ny]){
72                  add_edge(id[i][j],id[nx][ny],0);
73                  add_edge(id[nx][ny],id[i][j],0);
74             }
75             else{
76                  add_edge(id[i][j],id[nx][ny],1);
77                  add_edge(id[nx][ny],id[i][j],1);
78             }
79          }
80      }
81     int ans=1e9;
82     for(i=1;i<=n;i++)
83      for(j=1;j<=m;j++){
84         ans=min(ans,SPFA(id[i][j]));
85      }
86     printf("%d\n",ans);
87     return 0;
88 }
时间: 2024-10-13 02:46:11

CodeForces 37E Trial for Chief的相关文章

codeforces CF37E Trial for Chief BFS最短路

\(\rightarrow\) 戳我进CF原题 E. Trial for Chief time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard output Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chro

CF37E Trial for Chief(最短路)

题意 题意是给你一张 NMNMNM 的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数:(n,m<=50) 题解 转化为最短路.对于每一个点与它相邻的相同颜色的点连权值为0的边,对于颜色不同的点连权值为1的点.从每一个点跑单源最短路,把到W点的距离和到B点的距离+1取min作为此点的答案,最后把每一个点的答案取max就是答案. 对于能直接到达的两个颜色相同的点显然只用涂一次,如果不同就要再涂一次,所以这样建图.为了特判全是黑色的情况,所以到B点的距离要

Codeforces Gym 100513G G. FacePalm Accounting

G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/problem/G Description An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to partic

Codeforces Round #416

(?_?)最近脑子不好使(虽然一直都不太好用...) A题换了三个思路,全wa了(ó﹏ò?),改了差不多15遍都有了(菜的醉死...) 最后让老大给我看看,哇,错了好多地方???????. 大佬给我改好了代码,我自己又开始改,我的小宇宙要爆发了,还好最后也改对了(菜的要撞墙了 |墙|????:)); CodeForces - 811A A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 me

Codeforces Gym 100002 C &quot;Cricket Field&quot; 暴力

"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so

Codeforces Gym 100513G G. FacePalm Accounting 暴力

G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/problem/G Description An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to partic

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th