Paint the Grid Reloaded ZOJ - 3781 图论变形
| Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;
vector<int> c[];
char a[][];
int b[][],n,m,bcnt,vi[];
int w[][]= {{,},{-,},{,},{,-}};
void dfs(int r,int c1,int x)
{
b[r][c1]=x;
int i,rr,cc;
for(i=; i<; i++)
{
rr=r+w[i][];
cc=c1+w[i][];
if(rr<n&&rr>=&&cc<m&&cc>=&&!b[rr][cc]&&a[rr][cc]==a[r][c1])
{
dfs(rr,cc,x);
}
}
}
void build()
{
int i,j,k,rr,cc,x,y;
for(i=; i<=bcnt; i++)c[i].clear();
map<pair<int,int>,int>e;
e.clear();
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
for(k=; k<; k++)
{
rr=i+w[k][];
cc=j+w[k][];
//
if(rr<n&&rr>=&&cc<m&&cc>=&&b[rr][cc]!=b[i][j])
{
x=b[rr][cc],y=b[i][j];
if(x>y)swap(x,y);
e.insert(make_pair(make_pair(x,y),));
}
}
}
}
for(map<pair<int,int>,int>::iterator it=e.begin();it!=e.end();it++)
{
x=(*it).first.first,y=(*it).first.second;
c[x].push_back(y);
c[y].push_back(x);
//cout<<(*it).first.first<<" "<<(*it).first.second<<endl;
}
}
void DFS()
{
memset(b,,sizeof(b));
int i,j;
bcnt=;
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
if(!b[i][j])
dfs(i,j,bcnt++);
}
}
build();
/* for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}*/
}
int solve(int x)
{
memset(vi,,sizeof(vi));
queue<pair<int,int> >q;
while(!q.empty())q.pop();
pair<int,int>now;
q.push(make_pair(x,));
vi[x]=;
int i;
now.second=;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=;i<c[now.first].size();i++)
{
if(!vi[c[now.first][i]])
q.push(make_pair(c[now.first][i],now.second+)),vi[c[now.first][i]]=;
}
}
return now.second;
}
int main()
{
int t,i,mina;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=; i<n; i++)scanf("%s",a[i]);
DFS();
mina=;
for(i=;i<bcnt;i++)
mina=min(mina,solve(i));
printf("%d\n",mina);
}
}
Paint the Grid Reloaded ZOJ - 3781 图论变形的更多相关文章
- 【最短路+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 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 ...
- 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构成的格子,对 ...
- Paint the Grid Again ZOJ - 3780 拓扑
Paint the Grid Again Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu [ ...
- 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 ...
- 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( ...
随机推荐
- BackgroundWorker类中主要属性、方法和事件
属性: 1.CancellationPending 获取一个值,指示应用程序是否已请求取消后台操作.通过在DoWork事件中判断CancellationPending属性可以认 ...
- 在ubantu上安装hive
上次我们在ubantu上安装了hadoop,那我们现在再进一步,开始我们的学习之旅--hive板块吧! 第一步:准备! 软件: I.一个搭建好的hadoop环境 II.hive的bin文件(前往apa ...
- MVC配置伪静态
提出需求 伪静态能提高搜索引擎收录,还不影响硬盘寿命,是一个不错的选择,但是会增加CPU和内存开销,由于时候也需要实现伪静态. web.config配置 <system.webServer> ...
- Android学习记录:获取联系人
在AndroidManifest中申请读取联系人的权限 <uses-permission android:name = "android.permission.READ_CONTACT ...
- 转:【Java并发编程】之一:可重入内置锁
每个Java对象都可以用做一个实现同步的锁,这些锁被称为内置锁或监视器锁.线程在进入同步代码块之前会自动获取锁,并且在退出同步代码块时会自动释放锁.获得内置锁的唯一途径就是进入由这个锁保护的同步代码块 ...
- 【Alpha】Daily Scrum Meeting——Day6
站立式会议照片 1.本次会议为第五次Meeting会议: 2.本次会议在上午大课间09:40,在禹州楼召开,本次会议为30分钟讨论昨天的任务完成情况以及接下来的任务安排. 燃尽图 每个人的工作分配 成 ...
- 201521123056 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123121 《JAVA程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123008<java程序设计>第三周实验总结
1.本周学习总结 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; pub ...
- 201521123042 《Java程序设计》第12周学习总结
本次作业参考文件 正则表达式参考资料 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String ...