ZOJ 3781 Paint the Grid Reloaded
枚举,$BFS$,连通块缩点。
可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离。连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可。
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std; int T,n,m;
char s[][];
int Belong[][],block,use[*];
int dx[]={,,-,};
int dy[]={,-,,}; int h[*];
struct Edge
{
int from,to,nx;
}e[];
int sz; bool ok(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m) return ;
return ;
} void dfs(int x,int y)
{
Belong[x][y]=block;
if(ok(x-,y)&&s[x][y]==s[x-][y]&&Belong[x-][y]==) dfs(x-,y);
if(ok(x+,y)&&s[x][y]==s[x+][y]&&Belong[x+][y]==) dfs(x+,y);
if(ok(x,y-)&&s[x][y]==s[x][y-]&&Belong[x][y-]==) dfs(x,y-);
if(ok(x,y+)&&s[x][y]==s[x][y+]&&Belong[x][y+]==) dfs(x,y+);
} void add(int x,int y)
{
e[sz].from=x; e[sz].to=y; e[sz].nx = h[x];
h[x]=sz++;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) scanf("%s",s[i]); memset(Belong,block=,sizeof Belong);
sz=;
memset(h,0xff,sizeof(h));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(Belong[i][j]) continue;
block++; dfs(i,j);
}
} for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
for(int k=;k<;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(!ok(x,y)) continue;
if(Belong[i][j]==Belong[x][y]) continue;
add(Belong[i][j],Belong[x][y]);
}
}
} int ans=0x7fffffff;
for(int i=;i<=block;i++)
{
memset(use,-,sizeof(use));
int mx=;
use[i]=;
queue<int>q;
q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int j=h[x];j!=-;j=e[j].nx)
{
int y=e[j].to;
if(use[y]!=-) continue;
use[y]=use[x]+;
mx=max(use[y],mx);
q.push(y);
}
}
ans=min(mx,ans);
}
printf("%d\n",ans);
}
return ;
}
ZOJ 3781 Paint the Grid Reloaded的更多相关文章
- ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)
Paint the Grid Reloaded Time Limit: 2 Seconds Memory Limit: 65536 KB Leo has a grid with N rows ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
- ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- 【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781
题目: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...
- ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)
Paint the Grid Again Time Limit: 2 Seconds Memory Limit: 65536 KB Leo has a grid with N × N cel ...
随机推荐
- MongoDB入门(3)- MongoDB备份与恢复
1. 备份 MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下: mongodump.exe -h localhost -d database_name -o d:\ ...
- 【Android】完善Android学习(五:API 3.2)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- Codeforces Round #484 (Div. 2)
题目链接:http://codeforces.com/contest/982 A. Row time limit per test:1 second memory limit per test:256 ...
- Yii2实现读写分离(MySQL主从数据库)
读写分离(Read/Write Splitting). 1.原理: 让主数据库(master)处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库(slave)处理SELE ...
- linux 下 /bin /sbin 的区别 -- (转)
/bin,/sbin,/usr/bin,/usr/sbin区别 / : this is root directory root 用户根目录 /bin : command ...
- Windows Resizer
Windows ResizerWindows Resizer是chrome浏览器插件,可以调整视口大小
- 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)
简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html
- 2017多校第7场 HDU 6128 Inverse of sum 推公式或者二次剩余
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/a ...
- RSA加密解密与加签验签
RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年7月首次在美国公布 ...
- ASP.NET Core学习链接
https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html http://www.cnblogs.com/ar ...