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( ...
随机推荐
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(86)-日程管理-fullcalendar插件用法
前言 本文分享fullcalendar用法,最后面提供代码下载 说到日程管理,基于JQuery的插件FullCalendar当之无愧,完整的API稳定和调用方式,非常易于扩展!可以用于系统的个人历程管 ...
- JS面向对象编程(进阶理解)
JS 面向对象编程 如何创建JS对象 JSON语法声明对象(直接量声明对象) var obj = {}; 使用 Object 创建对象 var obj = new Object(); JS对象可以后期 ...
- 关于 ThinkPHP 在 Nginx 服务器上 使用U方法跳转问题
这个问题已多次遇到,关于tp 框架 使用U 方法跳转, 在Nginx 服务器上可能会遇到路由跳转不过去前面带点(如:./xx) 解决这个问题,可以在tp的入口文件 index.php 里定义个常量 d ...
- Jquery的入门学习
jQuery API中文文档地址 http://www.jquery123.com/ Jquery w3school教程 http://www.w3school.com.cn/jquery/index ...
- [UWP]理解及扩展Expander
##1. 前言 最近在自定义Expander的样式,顺便看了看它的源码. Expander控件是一个ContentControl,它通过IsExpanded属性或者通过点击Header中的Toggle ...
- 关于javascript原型链的个人理解
首先js是一种面对对象的语言,虽然大多数时候是以面对过程的形式展现出来.先来看一段代码: function Base() { this.name = 'tarol'; } function Sub() ...
- 201521123091 《Java程序设计》第9周学习总结
Java 第九周总结 第九周的作业. 目录 1.本章学习总结 2.Java Q&A 3.码云上代码提交记录及PTA实验总结 1.本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异 ...
- 201521123055 《Java程序设计》第5周学习总结
1. 本章学习总结 2. 书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 1. ...
- 201521123089 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 1.代码阅读:Child压缩包内源代码1.1 com.parent包中Child.java文件能否编译通过?哪 ...
- 201521123089 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 总结: (1)类名的首字母一定要大写. (2)制类型转换:类强制转换为子类时只有当引用类型真 ...