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( ...
随机推荐
- pythonl练习
练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式 ------------ info of Egon ----------- Name : Egon Age : 22 Sex : male ...
- iOS 环信集成问题(连文档都不说明的坑。。)
首先,关于环信SDK的下载和一些依赖库的添加,在此我就不做详细介绍,(http://www.easemob.com/download/im)附上环信官网文档,可以看一下,上面都可以下载,也有相关配置介 ...
- linq 起源
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- 集美大学网络1413第十四次作业成绩(团队九) -- 测试与发布&博客展示(Beta版本)
题目 团队作业9--测试与发布(Beta版本) 团队作业9成绩 团队/分值 Beta版本测试报告 Beta版本发布说明 总分 Bug类别. 数量 场景测试 测试结果 测试矩阵 出口条件 ...
- 201521123107 《Java程序设计》第7周学习总结
第7周作业-集合 1.本周学习总结 2.书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: public boolean contains( ...
- java--整理下关于static关键字的知识
如果将域定义为static,每个类中只有一个这样的域.而每一个对象对于所有的实例域却都有自己的一份拷贝.--<java核心技术> 使用static的两种情形:1.只想为某特定域分配单一存储 ...
- 201521123118《java程序与设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123025<<java程序设计>>第4周学习总结
Q1. 本周学习总结 Q2.书面作业 1.注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 2.面向对象设计(大作业1,非常重要) 2.1 将在网上 ...
- 201521123067 《Java程序设计》第3周学习总结
201521123067 <Java程序设计>第3周学习总结 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用 ...
- 201521123099 《Java程序设计》第2周学习总结
1. 本周学习总结 1.Java对我来说难度还是很大.需要花更多时间去学习. 2. 书面作业 1.使用Eclipse关联jdk源代码,并查看String对象的源代码(截图)?分析String使用什么来 ...