zoj3781
赛场上堵在了缩点上emmmmm
把原始图相同颜色的方块缩成一个点,然后与它周围不同颜色的联通块连双向边,然后枚举每个点然后求最大深度的最小值
因为每次翻转都相当于深度+1(可以手动模拟一下

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 100010
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std;
int T;
int n,m;
int deep[];
char a[][];
bool b[][];
int col[][];
bool vis[][],v[];
int cnt;
int ans;
queue<int>q; struct node{
int n;
node *next;
}*e[]; void in(int &x){
int y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(int x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void clean(){
For(i,,n)
For(j,,m){ vis[i][j]=;
col[i][j]=;
}
For(i,,n*m)
For(j,,n*m){
b[j][i]=;
b[i][j]=;
} For(i,,)
e[i]=;
cnt=;
ans=inf; } void push(int x,int y){
node *p;
p=new node();
p->n=y;
if(e[x]==)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} void dfs(int x,int y,char f,int color){
if(x<||y<||x>n||y>m)
return;
if(vis[x][y])
return;
if(a[x][y]==f){
vis[x][y]=;
col[x][y]=color;
dfs(x-,y,f,color);
dfs(x,y-,f,color);
dfs(x,y+,f,color);
dfs(x+,y,f,color);
}
} void ccc(int x,int y){
if(x<||y<||x>n||y>m)
return;
vis[x][y]=;
if(x->&&!b[col[x][y]][col[x-][y]]){
if(col[x][y]!=col[x-][y]){
push(col[x][y],col[x-][y]);
push(col[x-][y],col[x][y]);
b[col[x][y]][col[x-][y]]=;
b[col[x-][y]][col[x][y]]=;
}
if(vis[x-][y])
ccc(x-,y);
}
if(y->&&!b[col[x][y]][col[x][y-]]){
if(col[x][y]!=col[x][y-]){
push(col[x][y],col[x][y-]);
push(col[x][y-],col[x][y]);
b[col[x][y]][col[x][y-]]=;
b[col[x][y-]][col[x][y]]=;
}
if(vis[x][y-])
ccc(x,y-);
}
if(x+<=n&&!b[col[x][y]][col[x+][y]]){
if(col[x][y]!=col[x+][y]){
push(col[x][y],col[x+][y]);
push(col[x+][y],col[x][y]);
b[col[x][y]][col[x+][y]]=;
b[col[x+][y]][col[x][y]]=;
}
if(vis[x+][y])
ccc(x+,y);
}
if(y+<=m&&!b[col[x][y]][col[x][y+]]){
if(col[x][y]!=col[x][y+]){
push(col[x][y],col[x][y+]);
push(col[x][y+],col[x][y]);
b[col[x][y]][col[x][y+]]=;
b[col[x][y+]][col[x][y]]=;
}
if(vis[x][y+])
ccc(x,y+);
}
} void bfs(int x){
deep[x]=;
q.push(x);
For(i,,cnt)
v[i]=;
v[x]=;
int ss=;
while(!q.empty()){
int t=q.front();q.pop();
for(node *i=e[t];i;i=i->next)
if(!v[i->n]){
v[i->n]=;
q.push(i->n);
deep[i->n]=deep[t]+;
ss=max(ss,deep[i->n]);
}
}
ans=min(ss,ans);
} int main(){
in(T);
while(T--){
in(n);in(m);
clean();
For(i,,n)
For(j,,m)
cin>>a[i][j];
For(i,,n)
For(j,,m)
if(!vis[i][j])
dfs(i,j,a[i][j],++cnt);
ccc(,);
For(i,,cnt)
bfs(i);
o(ans);p('\n');
}
return ;
}

zoj3781的更多相关文章

  1. [ZOJ3781]Paint the Grid Reloaded

    思路: 先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$. 注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上. 一开始和网上的题解对拍$T=1$ ...

随机推荐

  1. hihoCoder #1770 : 单调数(数位dp)

    题面 我们定义一个数是单调数,当且仅当构成这个数每一个数位都是单调不降或不增的. 例如 \(123\) 和 \(321\) 和 \(221\) 和 \(111\) 是单调的,而 \(312\) 不是单 ...

  2. 【BZOJ5498】[十二省联考2019]皮配(动态规划)

    [BZOJ5498][十二省联考2019]皮配(动态规划) 题面 BZOJ 洛谷 题解 先考虑暴力\(dp\),设\(f[i][j][k]\)表示前\(i\)所学校,有\(j\)人在某个阵营,有\(k ...

  3. ovs之组网实验

    介绍 本示例将创建两个OVS实例和两个主机,其中每个OVS上接入一个主机,OVS实例之间有链路连接,形成一个链状拓扑,如图.在OVS组网完成之后,再通过手动方式添加流表,实现网络通信,从而验证实验可行 ...

  4. Codeforces Round #530 (Div. 2) C D

    C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...

  5. 和CISSP并肩的信息安全认证国际注册信息安全经理CISM

    众所周知,信息安全认证界有一个扛把子的证书叫CISSP(国际信息安全专家认证),一般拥有CISSP证书的小哥哥还会选择考取另一个认证,这就是今天给大家介绍的CISM(国际注册信息安全经理).CISM是 ...

  6. pandas的分布式执行框架之modin

    Scale your pandas workflows by changing one line of code       To use Modin, replace the pandas impo ...

  7. Exponent CMS 2.3.9 配置文件写入 Getshell分析

    在 install/index.php 的第44行 expString::sanitize($_REQUEST); 跟进sanitize函数 public static function saniti ...

  8. NOI-OJ 2.2 ID:1696 逆波兰表达式

    思路 很容易看出规律,一个运算符出现,其后就一定需要左值和右值,而左值和右值有可能还是运算符,这就需要继续递归.递归终止的条件就是遇到数字. 逆波兰表达式其实是构造成了一颗二叉树 例程 #includ ...

  9. [物理学与PDEs]第3章第2节 磁流体力学方程组 2.2 考虑到电磁场的存在对流体力学方程组的修正

    1.  连续性方程 $$\bex \cfrac{\p \rho}{\p t}+\Div(\rho{\bf u})=0.  \eex$$ 2.  动量守恒方程 $$\bex \cfrac{\p }{\p ...

  10. sql注入学习 sqlliab教程 lesson1 (sqlliab搭建教程)

    靶场搭建 小白建议直接用集成环境.推荐laragon (由于这套靶场较早,需要使用php7.0以下环境,安装完php laragon需要在安装php低版本,默认laragon只集成了一个7.0的php ...