zjuoj 3773 Paint the Grid
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773
Paint the Grid
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
Leo has a grid with N × N cells. He wants to paint each cell either black or white.
After he finished painting, the grid will be divided into several parts. Any two connected cells should be in the same part, and any two unconnected cells should be in different parts. Two cells are connected if they share an edge and they are in the same color. If two cells are connected with the same another cell, the two cells are also connected.
The size of a part is the number of cells in it. Leo wants to have at least ⌊N×4÷3⌋ different sizes (⌊x⌋ is the maximum integer which is less than or equal to x).
Can you tell him how to paint the grid?
Input
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
There is one integer N (4 <= N <= 100).
Output
For each test case, output a solution to painting. You should output exactly N lines with each line contains N characters either 'X' (black) or 'O' (white). See the sample output for details.
This problem is special judged so any correct answer will be accepted.
Sample Input
1
5
Sample Output
XOXXX
OOOOO
XXXXX
OXXOO
OXXOO
Author: ZHOU, Yuchen
Source: The 14th Zhejiang University Programming Contest
分析:
给你一个染色的 N×M 的格子,你每次可以选择一个连通块,将其颜色取反。问最后将整个格子变为同色的最小步数。
思路:
其实就是一个最短路,将连通块缩点然后不同种的连通块之间连边,那么 将整个格子变为同色的最小步数就等于一个点到地图上最远点的距离了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:1024000000,1024000000")
#define maxn 45
#define MAXN 2700005
#define OO (1<<31)-1
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; int n,m,ans,cnt,lev;
char mp[maxn][maxn];
int num[maxn][maxn];
int dx[]={,,-,};
int dy[]={-,,,}; bool vis[maxn][maxn],app[][];
int p[];
struct Node
{
int v;
int next;
}edge[MAXN]; void addedge(int u,int v)
{
cnt++;
edge[cnt].v=v;
edge[cnt].next=p[u];
p[u]=cnt;
}
bool isok(int x,int y)
{
if(x<||x>n||y<||y>m) return false ;
return true ;
}
void dfs(int x,int y)
{
num[x][y]=lev;
int nx,ny,i;
for(i=;i<;i++)
{
nx=x+dx[i]; ny=y+dy[i];
if(isok(nx,ny)&&!vis[nx][ny]&&mp[nx][ny]==mp[x][y])
{
vis[nx][ny]=;
dfs(nx,ny);
}
}
}
void presolve()
{
memset(p,,sizeof(p));
int i,j,k,t,nx,ny;
lev=;
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(!vis[i][j])
{
lev++;
vis[i][j]=;
dfs(i,j);
}
}
}
cnt=;
memset(app,,sizeof(app));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
for(k=;k<;k++)
{
nx=i+dx[k]; ny=j+dy[k];
if(isok(nx,ny)&&num[nx][ny]!=num[i][j]&&!app[num[nx][ny]][num[i][j]])
{
app[num[nx][ny]][num[i][j]]=;
addedge(num[nx][ny],num[i][j]);
}
}
}
}
}
struct fuck
{
int dis;
int num;
}t,f;
bool VVV[];
queue<fuck> Q;
int bfs(int now)
{
memset(VVV,,sizeof(VVV));
t.dis=;
t.num=now;
VVV[now]=;
while(!Q.empty()) Q.pop();
Q.push(t);
int maxs=;
while(!Q.empty())
{
t=Q.front();Q.pop();
if(t.dis>ans) return INF;
for(int i=p[t.num];i!=;i=edge[i].next)
{
int to=edge[i].v;
if(!VVV[to])
{
VVV[to]=;
f.num=to;
f.dis=t.dis+;
maxs=max(maxs,f.dis);
Q.push(f);
}
}
}
return maxs;
}
int main()
{
int i,j,t,u,v,w;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
scanf("%s",mp[i]+);
}
presolve();
ans=INF;
for(int i=;i<=lev;i++)
{
ans=min(ans,bfs(i));
}
printf("%d\n",ans);
}
return ;
}
zjuoj 3773 Paint the Grid的更多相关文章
- zjuoj 3780 Paint the Grid Again
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...
- 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 ...
- 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 ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- Paint the Grid Again (隐藏建图+优先队列+拓扑排序)
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...
- 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 - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds Me ...
- ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)
Paint the Grid Again Time Limit: 2 Seconds Memory Limit: 65536 KB Leo has a grid with N × N cel ...
随机推荐
- bzoj 3791: 作业
Description 众所周知,白神是具有神奇的能力的. 比如说,他对数学作业说一声“数”,数学作业就会出于畏惧而自己完成:对语文作业说一声“语”,语文作业就会出于畏惧而自己完成. 今天,语文老师和 ...
- 卸载oracle删除注册表脚本
一.前言 在我们操作系统中,有时要卸载oracle数据库,每一次都要去删除win下的注册表,为了方便删除注册表的信息,下面通过一种删除注册表快捷的脚本. 二.脚本信息 Windows Registry ...
- Why do we live in this world?
Why do we live in this world? It seems to me there is nothing but two reasons, - to live the livabil ...
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似. 1.建立字符串资源文件strings.xml: <resources> ...
- CSS生僻问题一网打尽
CSS生僻问题一网打尽 伪类和伪元素 伪类 何为伪类? 像类不是类,不是自己声明的类(不写样式也存在). 对伪元素的认识在早期网页上的超链接.链接(锚啊)用下划线标出来,点击后链接变紫色,鼠标悬上去变 ...
- 转:Maven常用命令
转:Maven常用命令 Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ Maven常用命令: 1 ...
- python 进程和线程
python中的进程.线程(threading.multiprocessing.Queue.subprocess) Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就 ...
- 转-HttpClient4.3 连接管理
转 http://www.yeetrack.com/?p=782 2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大 ...
- 批处理命令 BAT备份MySQL数据库
批处理命令 BAT备份MySQL数据库 作者: 字体:[增加 减小] 类型:转载 时间:2009-07-23我要评论 MySQL数据的备份工具也许有很多,在这我要给大家分享一下通过DOS批处理命令和M ...
- 史上自定义 JavaScript 函数Top 10
http://www.dustindiaz.com/top-ten-javascript/ 发布:wpulog | 发布时间: 2010年4月9日 10个被使用的最普遍的用户自定义函数,add ...