【最短路+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 initially.
Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both A and B.
Leo wants to paint the grid with the same color. He can make it done in multiple steps. At each step Leo can choose a cell and flip the color (from black to white or from white to black) of all cells connected to it. Leo wants to know the minimum number of steps he needs to make all cells in the same color.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N and M (1 <= N, M <= 40). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the initial color of the cells.
Output
For each test case, output the minimum steps needed to make all cells in the same color.
Sample Input
2
2 2
OX
OX
3 3
XOX
OXO
XOX
Sample Output
1
2
Hint
For the second sample, one optimal solution is:
Step 1. flip (2, 2)
XOX
OOO
XOX
Step 2. flip (1, 2)
XXX
XXX
XXX
大意:
给定的图片有两种颜色,我们可以任选一个色块将其颜色改变,求最少几次能把图片变为纯色。
题解:
题目的数据最大是40,不算很大,大概率可暴力
首先因为图片形式的数据并不好处理,所以我们把它分为最小的影响部分,把每一个单独的色块缩成一个点,边界相邻的变成一条线,这样就吧一个图片转换成图了。
实现方法:先用bfs对每个色块涂色并编号,然后再用bfs遍历色块,转换成图。两者复杂度都很低O(n*m)
然后问题就化简为将图变为纯色的最小步骤了。
这时图中相邻的点一定是异色的,因为如果两点相邻切同色就会被归为一点。
再思考涂色的方法,对任意点转色后,相当于把该点的所有邻点变为同色,也就是说相当于把所有邻点缩为一个新点,之前被缩的点的所有边变为新点的边。假设一个点的邻点的平均值是x,新点的邻点的平均值就是x*x。根据贪心原则下一个选择转色的点也应该是新点。所以转色就变成了从某点开始图的深度。
这样题目就变成了,求图深度的最小值。
实现方法:bfs遍历各点。
AC代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#define clear(x) {memset(x, 0, sizeof(x));}
using namespace std;
struct point {
int x, y;
};
int dx[] = { ,,,- };
int dy[] = { ,-,, };
char s[][];
int book[][];
int book2[][];
vector<int>e[];
int book3[];
int k, n, m,ma;
queue<point>q;
int bfs1(point x) {
if (book[x.x][x.y] != )return ;
while (!q.empty())q.pop();
q.push(x);
book[x.x][x.y] = k;
while (!q.empty()) {
for (int i = ; i < ; i++) {
point tmp = q.front();
tmp.x += dx[i];
tmp.y += dy[i];
if (tmp.x < || tmp.x >= n)continue;
if (tmp.y < || tmp.y >= m)continue;
if (book[tmp.x][tmp.y] == &&s[q.front().x][q.front().y]==s[tmp.x][tmp.y]) {
book[tmp.x][tmp.y] = k;
q.push(tmp);
}
}
q.pop();
}
return ;
} void bfs2(point x){
if (book2[x.x][x.y] != )return;
while (!q.empty())q.pop();
q.push(x);
book2[x.x][x.y] = ;
while (!q.empty()) {
for (int i = ; i < ; i++) {
point tmp = q.front();
tmp.x += dx[i];
tmp.y += dy[i];
if (tmp.x < || tmp.x >= n)continue;
if (tmp.y < || tmp.y >= m)continue;
if (book2[tmp.x][tmp.y] == && s[q.front().x][q.front().y] == s[tmp.x][tmp.y]) {
book2[tmp.x][tmp.y] = ;
q.push(tmp);
}
else if (s[q.front().x][q.front().y] != s[tmp.x][tmp.y]) {
e[book[q.front().x][q.front().y]].push_back(book[tmp.x][tmp.y]);
}
}
q.pop();
}
}
int bfs3(int top) {
int max = ;
clear(book3);
while (!q.empty())q.pop();
q.push({ top, });
book3[top] = ;
while (!q.empty()) {
point tmp = q.front();
if (tmp.y > max)max = tmp.y;
for (int i :e[tmp.x]) {
if (book3[i] == ) {
book3[i] = ;
q.push({ i,tmp.y + });
}
}
q.pop();
}
return max; } int main() {
int t;
cin >> t;
while (t--) { int ans = 1e9;
k = ;
cin >> n >> m;
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
cin >> s[i][j]; for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
if(bfs1({ i,j }))k++; for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
bfs2({ i,j }); for (int i = ; i < k; i++) {
int tmp = bfs3(i);
if (tmp < ans)ans = tmp;
} cout << ans << '\n'; clear(book);
clear(book2);
clear(e);
} }
【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781的更多相关文章
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- 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 - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- Paint the Grid Reloaded(缩点,DFS+BFS)
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...
- 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS
原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...
- 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 ...
- Paint the Grid Again ZOJ - 3780 拓扑
Paint the Grid Again Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu [ ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- [ZOJ3781]Paint the Grid Reloaded
思路: 先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$. 注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上. 一开始和网上的题解对拍$T=1$ ...
随机推荐
- Magicodes.IE在.NET Core中通过请求头导出多种格式文件
前言 在2.2里程碑中我们增加了一些新的功能,正如标题所写通过请求头进行导出我们不同格式的文件.下面我们来看一下如何使用.通过这种方式无论是对我们的数据多用途,还是说对我们的数据校验都做到了轻松易配. ...
- Jmeter系列(31)- 获取并使用 JDBC Request 返回的数据
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 前言 Jmeter 使用 JDBC R ...
- 云服务器终端命令显示-bash-4.2#怎么解决
原因:删除了root/.bashrc 和 root/.bash_profile两个文件的丢失 解决办法: -bash-4.2# cp /etc/skel/.bashrc /root/ -bash-4. ...
- dart快速入门教程 (7.4)
7.12.多态 多态字面上理解就是多种状态,通俗的说,多态表现为父类定义一个方法不去实现,子类继承这个方法后实现父类的方法,这个方法有多种表现 // import 'person.dart'; voi ...
- [源码解析]Oozie来龙去脉之提交任务
[源码解析]Oozie来龙去脉之提交任务 0x00 摘要 Oozie是由Cloudera公司贡献给Apache的基于工作流引擎的开源框架,是Hadoop平台的开源的工作流调度引擎,用来管理Hadoop ...
- SpringBoot中Service实现类添加@Service却任然无法注入的问题
最近一直在研究Spring Boot.从GitHub上下载了一个my-Blog源码,一边看,一边自己尝试去实现,结果掉在坑了,研究了近一周才爬出来,特地来这博客园记录下来,一是避免自己在放这样的错误, ...
- CSS中那些必须掌握的概念
一.盒子模型 1.什么是盒子模型 css盒模型本质上是一个盒子,封装周围的html元素,它包括:外边距(margin).边框(border).内边距(padding).实际内容(content)四个属 ...
- oracle 环境变量配置 字符集编码配置
字符编码的环境变量配置: http://jingyan.baidu.com/article/e73e26c0c20f1a24adb6a73e.html (1).数据库服务器字符集select * fr ...
- 每天一个LINUX命令(pwd)
每天一个LINUX命令(pwd) 基本信息 pwd: /bin/pwd,显示当前路径的绝对路径 语法:pwd 应用程序位置 which pwd PWD作用 pwd --help ...
- 每日一题 - 剑指 Offer 48. 最长不含重复字符的子字符串
题目信息 时间: 2019-07-02 题目链接:Leetcode tag: 动态规划 哈希表 难易程度:中等 题目描述: 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度 ...