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. 【BZOJ5496】[十二省联考2019]字符串问题(后缀树)

    [BZOJ5496][十二省联考2019]字符串问题(后缀树) 题面 BZOJ 洛谷 题解 首先显然可以把具有支配关系的串从\(A\)到\(B\)连一条有向边,如果\(B_i\)是\(A_j\)的前缀 ...

  2. go 的包

  3. ElasticSearch 索引 剖析

    ElasticSearch index 剖析 在看ElasticSearch权威指南基础入门中关于:分片内部原理这一小节内容后,大致对ElasticSearch的索引.搜索底层实现有了一个初步的认识. ...

  4. [再寄小读者之数学篇](2015-06-24 Series)

    (AMM. Problems and Solutions. 2015. 03) Let $\sed{a_n}$ be a monotone decreasing sequence of real nu ...

  5. ORACLE使用CASE WHEN的方法

    先写草稿. 说下我的需求,ORACLE数据库有两个字段RECEIVER_MOBILE与RECEIVER_PHONE,同为联系方式.当RECEIVER_MOBILE为空的时候,需要用到RECEIVER_ ...

  6. SrpingBoot部署到云服务器

    预先准备事项 1.本地主机:安装maven 2.云端主机:安装和配置jdk 一.maven打包 方式一:maven手动版 切换至项目下,cmd:mvn package 查看target目录: 方式二: ...

  7. 关于模拟I2C的一些问题???

    1.在调试BH1750时发现stm32f103rb单片机用模拟I2C通讯时引脚使用开漏模式能正常读出来数据,使用推挽模式则完全无法通讯,发送地址后从机没有应答? https://blog.csdn.n ...

  8. 1120 机器人走方格 V3(组合数)

    题目实际上是求catalan数的,Catalan[n] = C(2*n,n) / (n+1) = C(2*n,n) % mod * inv[n+1],inv[n+1]为n+1的逆元,根据费马小定理,可 ...

  9. LOJ 3049: 洛谷 P5284: 「十二省联考 2019」字符串问题

    题目传送门:LOJ #3049. 题意简述: 给定一个长度为 \(n\) 的母串 \(S\). 有 \(n_a\) 个 A 类串,都是 \(S\) 的子串,以区间的形式给出. 有 \(n_b\) 个 ...

  10. Beta 冲刺(4/7)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(4/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 整理博客 ppt模板 接下来的计划 做好机动. ...