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 ...
随机推荐
- 玩转GIT
远程代码覆盖本地 解决方法: 1.如果还没有 commit 的话,可以用 git checkout . 这样将使所有代码还原到最后一次 commit 的状态 2.如果已经 commit 了,最简单的方 ...
- Linux CentOS6.5下安装Oracle ASM
Oracle版本:Oracle 11g 1.确定自己的Linux版本: [root@localhost ~]#uname -r 2.6.32-431.el6.x86_64 2.6.32-431.el6 ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 03.深入javascript
函数返回值 函数返回值和函数传参正好相反,函数传参是我们可以把一些东西传到函数里面去,函数返回值是函数可以把一些东西传到外面来. <script> function show() { re ...
- html5新特性
这一篇博文不会告诉你怎么去使用html5的新特性,只会给你总结一下新特性------对于好学的人可以把这篇文章当做一个目录 对于初接触的人来说是一个导向 对于已经接触过的人来说是一个检测你掌握程度的检 ...
- php操作数据库的简单示例
放假期间自己又写了几个简单的网页,但在服务器中打开时和在网站上打开时不一样,在服务器中打开的出现了错误,字体比一般的腰大好多,页面也相应地变大了,一些块即使用了浮动和clear浮动还是被遮住了,我只好 ...
- 轻量级队列beanstalkd
一.基本Beanstalkd,一个高性能.轻量级的分布式内存队列系统,最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web应用系统的页面访问延迟,支持过有9.5 million用户的Face ...
- POJ2965
#include <stdio.h> char map[4][4]; int map1[4][4]; int map2[4][4]; int num[16]; int min=1000,n ...
- JBoss QuickStart之深入
EJB-AsynchronousEJB中提供异步调用的方法. "A session bean can expose methods with asynchronous client invo ...
- openstack中eventlet使用
openstack中使用eventlet的协程来实现并发. 第一种,使用eventlet.GreenPool来管理绿色线程 如l3-agent在开启了8个绿色线程来处理router消息 def _pr ...