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 ...
随机推荐
- 互联网+下PDA移动智能手持POS超市收银开单软件
是一套专为中小超市.专卖店设计的收银管理软件,广泛应用于中小超市(百货商店).化妆品店.婴幼儿用品店.玩具店.保健品店.茶叶店. 电器.文具图书.手机通讯器材店等行业的中小型店面店铺.该系统具有完善的 ...
- [工作中的设计模式]装饰模式decorator
一.模式解析 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的要点主要是: 1.需要对已有对象扩展新的功能,又不希望改变原有对 ...
- Django+Tastypie作后端,RequireJS+Backbone作前端的TodoMVC
一.配置好环境 接着前一篇的例子,顺带测试一下已下载下来example里面的backbone_require的例子 注意:直接本地用backbone.localStorage插件运行TodoMVC会报 ...
- 【改】iOS学习之NSAttributedString(富文本)
NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...
- Bigtable 论文 阅读笔记 - 原理部分
不支持markdown,桑心.更好的阅读体验请看:Github/Bigtable.md Paper: Google Bigtable paper Notes author: Lhfcws Wu Tim ...
- C#中的简单工厂和单例
下面首先来说说简单工厂 举个例子: 首先是父类 public abstract class Pizza { public abstract string Info(); } } 子类 public c ...
- Cannot instantiate the type AppiumDriver
I have added following jars in my projects build path: java-client-2.0.0 from http://appium.io/downl ...
- javascript中矩形的碰撞检测---- 计算碰撞部分的面积
今天在做一个拖拽改变元素排序的东西的时候,在做被拖动元素同时碰撞到两个元素时,究竟应该与哪个元素交换位置的问题上,纠结到崩溃,实在是想不到别的办法去做了,只能去想办法计算碰撞的面积. 这应该不是最合适 ...
- 掌握Thinkphp3.2.0----内置标签
使用内置标签的时候,一定要注意闭合-----单标签自闭合,双标签对应闭合 标签的学习在于记忆和应用 一. 判断比较 //IF 语句的完整格式 <if condition="$user ...
- Online, Asynchronous Schema Change in F1
F1: A Distributed SQL Database That Scales http://disksing.com/understanding-f1-schema-change ma ...