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 ...
随机推荐
- find - 递归地在层次目录中处理文件
总览 SYNOPSIS find [path...] [expression] 描述 DESCRIPTION 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它 ...
- 2018.6.19 Java核心API与高级编程实践复习总结
Java 核心编程API与高级编程实践 第一章 异常 1.1 异常概述 在程序运行中,经常会出现一些意外情况,这些意外会导致程序出错或者崩溃而影响程序的正常执行,在java语言中,将这些程序意外称为异 ...
- MFC-[转]基于MFC的ActiveX控件开发
作者:lidan | 出处:博客园 | 2012/3/13 16:10:34 | 阅读22次 ActiveX 控件是基于组件对象模型 (COM) 的可重用软件组件,广泛应用于桌面及Web应用中.在VC ...
- 工具之UltraEdit之正则表达式
- 【Python学习之九】asyncio—异步IO
asyncio 这是python3.4引入的标准库,直接内置对异步IO的支持.asyncio的编程模型就是一个消息循环.从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程 ...
- JZOJ 1265. Round Numbers
1265. Round Numbers(rndnum.pas/c/cpp) (File IO): input:rndnum.in output:rndnum.out Time Limits: 1000 ...
- 排序算法合集(Java)
整理了一下常用的排序算法,算法过程和示意图不详细讲,百度很多,只列代码,如有错误,还望大家指出. 1.冒泡排序 public static void bubbleSort(int[] a){ for( ...
- python-PIL模块的使用
PIL基本功能介绍 from PIL import Image from PIL import ImageEnhance img = Image.open(r'E:\img\f1.png') img. ...
- Spring---浅谈AOP
概念 AOP是Aspect Oriented Programming的缩写,即面向切面的编程.是一种比较新颖的编程思想,也是Spring框架中一个重要的领域. AOP将应用系统分为两个部分:核心业务逻 ...
- Java面向对象---方法的创建与重载
方法的创建 方法就是可重复调用的代码段. 定义: 访问修饰符 返回值类型 方法名(参数){ 方法主体 } 返回值类型:void(无返回值):基本数据类型:应用数据类型:类对象等. 方法名的命名规则:第 ...