zoj3781
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的更多相关文章
- [ZOJ3781]Paint the Grid Reloaded
思路: 先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$. 注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上. 一开始和网上的题解对拍$T=1$ ...
随机推荐
- 20165223 《信息安全系统设计基础》 实现mybash
一.了解 mybash 1. 简介 bash 是 Bourne Again Shell 的缩写,是linux默认的标准shell(也是大家常说的系统内核),bash也是Unix/Linux上常见的 ...
- DP擎天
DP! 黄题: 洛谷P2101 命运石之门的选择 假装是DP(分治 + ST表) CF 982C Cut 'em all! 树形贪心 洛谷P1020 导弹拦截 单调队列水题 绿题: 洛谷P1594 护 ...
- 使用Spring-Integration实现http消息转发
目标:接收来自华为云的服务器报警信息,并转发到钉钉的自定义机器人中 使用Spring-Integration不仅节省了很多配置,还增加了可用性. 更多关于Spring-Integration的介绍可参 ...
- MyBatis集成到Spring时配置MapperScannerConfigurer出错
问题描述 在web项目中同时集成了spring mvc和mybatis. 将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入. ...
- Leetcode#521. Longest Uncommon Subsequence I(最长特殊序列 Ⅰ)
题目描述 给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符实现,但 ...
- JS获取当月第一天和最后一天
/** * 获取当前月的第一天 */function getCurrentMonthFirst(){ var date=new Date(); date.setDate(1); return date ...
- JSON 之 SuperObject(11): TSuperTableString、TSuperAvlEntry
JSON 之 SuperObject(11): TSuperTableString.TSuperAvlEntry - 万一 - 博客园http://www.cnblogs.com/del/archiv ...
- SpringBoot使用Redis共享用户session信息
SpringBoot引入Redis依赖: <dependency> <groupId>org.springframework.boot</groupId> < ...
- Linux环境下安装SQL Server 2017
参考链接 https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sql-server-2 ...
- Git入门——本地版本库操作
作为一个一直用SVN的家伙,深深地感到了自己在版本控制工具上的落伍.... 首先必须强调的是: Git与Github不是一回事. Git是目前世界上最先进的分布式版本控制系统,于2005年被linux ...