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 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
题目链接:ZOJ 3781
前段时间受同学邀请去打了一个小小的比赛,内容是11届浙江省赛题目(除去了某道现场AC率最低的那题),由于还是图样,最后仅五题滚粗,这种比赛对于我这种学过那么一点点算法的人但又不精通的人来说就是不会写,跟没学过算法的人来说没啥区别,果然还是太菜鸟了,这题比赛的时候只想到是BFS,然后硬是写了一个用map的暴力,结果内存爆了(意料之中),后来想想以为是字符只有两种,可能用某种压缩储存的办法,然而看了题解发现是普通的BFS,只是用到了缩点的思想(反正我想不出来,这办法真的很巧妙)。
可以这样想:题目显然是同样的颜色邻近的点是同一个连通分量中的,翻动任意一个会使得这整个连通分量都发生变化,那么这整个分量中的点均为等价的,然后枚举一开始翻的分量i,若在第一次翻i的条件下想全部翻为同色,则至少要找到那个离i最远的分量j,两者之间隔的距离就是至少翻过的次数,这样一来枚举每一个点进行BFS然后取其中dis[i][j]的最大值更新ans的最小值就好了。当然事先得暴力地把点缩一下
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 45;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[(N * N * 4) << 1];
int head[N * N], tot;
int d[N * N];
bitset<N*N>vis;
bool link[N * N][N * N];
char pos[N][N];
int id[N][N], dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}};
int n, m; void init()
{
CLR(head, -1);
tot = 0;
CLR(id, 0);
CLR(link, false);
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
void bfs(int s)
{
CLR(d, INF);
vis.reset();
queue<int>Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!vis[v])
{
vis[v] = 1;
d[v] = d[u] + 1;
Q.push(v);
}
}
}
}
inline bool check(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < m;
}
void dfs(int x, int y, int ID)
{
id[x][y] = ID;
for (int i = 0; i < 4; ++i)
{
int vx = x + dir[i][0];
int vy = y + dir[i][1];
if (check(vx, vy) && pos[vx][vy] == pos[x][y] && !id[vx][vy])
dfs(vx, vy, ID);
}
}
int main(void)
{
int tcase, i, j;
scanf("%d", &tcase);
while (tcase--)
{
init();
scanf("%d%d", &n, &m);
for (i = 0; i < n; ++i)
scanf("%s", pos[i]);
int cntid = 0;
for (i = 0; i < n; ++i)
for (j = 0; j < m; ++j)
if (!id[i][j])
dfs(i, j, ++cntid);
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
for (int k = 0; k < 4; ++k)
{
int ii = i + dir[k][0];
int jj = j + dir[k][1];
if (check(ii, jj) && pos[ii][jj] != pos[i][j] && !link[id[i][j]][id[ii][jj]] && !link[id[ii][jj]][id[i][j]])//link数组防止无用的重边
{
add(id[i][j], id[ii][jj]);
add(id[ii][jj], id[i][j]);
link[id[i][j]][id[ii][jj]] = link[id[ii][jj]][id[i][j]] = true;
}
}
}
}
int ans = n * m;
for (i = 1; i <= cntid; ++i)
{
bfs(i);
ans = min(ans, *max_element(d + 1, d + cntid + 1));
}
//cout<<tot<<endl;
printf("%d\n", ans);
}
return 0;
}
ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)的更多相关文章
- 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(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求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- ZOJ 3781 Paint the Grid Reloaded
枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- 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 ...
- 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 ...
随机推荐
- 解决session过期跳转到登录页并跳出iframe框架(或者layui弹出层)
当用户长时间停留在管理界面没有操作,等到session过期后,进行了操作,那么只是iframe跳转到login页面,这不是我们想要的结果.解决方法:在login页面加一个逻辑判断: <scrip ...
- spring-boot自定义启动端口
有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...
- AngularJs学习笔记-组件生命周期
组件生命周期 (1)组件生命周期钩子 constructor:组件创建时被创建 ngOnChanges: 父组件修改或初始化子组件的输入属性时被调用,如果子组件没有输入属性,则永远不会被调用,它的首次 ...
- Nginx学习记录(二)
1. 什么是反向代理 正向代理 反向代理: 反向代理服务器决定哪台服务器提供服务. 返回代理服务器不提供服务器.也是请求的转发. 反向代理(Reverse Proxy)方式是指以代理服务器来接受Int ...
- Java 性能优化的五大技巧
要对你的 Java 代码进行优化,需要理解 Java 不同要素之间的相互作用,以及它是如何与其运行时的操作系统进行交互的.使用下面这五个技巧和资源,开始学习如何分析和优化你的代码吧. 在我们开始之前, ...
- Oracle 函数 之 Coalesce()、greatest()、least()
Coalesce().greatest().least() oracle比较一列的数据大小时,我们一般使用max()/min()函数,比较一行的最大值或者最小值时,使用函数Coalesce()/gre ...
- Oracle下如何收集 Systemstate dump
2: dump (不包括lock element) 10: dump 11: dump + global cache of RAC 256: short stack (函数堆栈) 258: 256+2 ...
- v4l2解析
v4l2的学习建议和流程解析: http://www.cnblogs.com/silence-hust/p/4464291.html 补充: 枚举设备所支持的image format: VIDIOC_ ...
- BZOJ2118: 墨墨的等式(最短路 数论)
题意 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. So ...
- 【贪心 二分图 线段树】cf533A. Berland Miners
通过霍尔定理转化判定方式的一步还是很妙的 The biggest gold mine in Berland consists of n caves, connected by n - 1 transi ...