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的更多相关文章

  1. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  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 ...

  3. Paint the Grid Again ZOJ - 3780 拓扑

    Paint the Grid Again Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu [ ...

  4. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  9. 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 ...

随机推荐

  1. js无参数对象

    <script type="text/javascript"> var o={ a : function(){ for (var i = arguments.lengt ...

  2. java比较两个对象是否相等的方法

    java比较两个对象是否相等直接使用equals方法进行判断肯定是不会相同的. 例如: Person  person1  =new Person("张三"); Person  pe ...

  3. 如何在VS2012中使用IL Disassembler中查看项目编译生成的程序集

    2016-05-26 11:48:46 测试的WPF项目 MainWindow.xaml代码 MainWindow.xaml.cs 代码 在学习WPF的时候,想验证:删除MainWindow.xaml ...

  4. BZOJ3687 计算子集和的异或和

    题不知道怎么不见了,bzoj上已经没了3687这题了 题意:给你一个n 然后输入n个数 求这n个数的所有子集的和的异或和 思路:用bitset记录某个数是否在子集和中出现,利用bitset对二进制位的 ...

  5. node.js中module.export与export的区别。

    对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports ...

  6. 不安装Oracle客户端使用PLSQL连接Oracle数据库的方法

    1,下载PL\SQL http://dl8.cr173.com/soft1/PLSQLDeveloper10_ha.zip(这个是我下载的,带破解和汉化); 2,下载完后傻瓜式安装 ,这里说下,1是P ...

  7. linux服务器使用

    1.在widows系统下,下载putty.exe 配置默认的服务器IP + 端口 添加名称.点击save即可 参考:http://jingyan.baidu.com/article/c74d60004 ...

  8. 使用staruml学习画类图

    //这是startuml 把uml 转换成的java代码: public class Circle implements Ishape { private double _radius; public ...

  9. 手动编译安装LNMP

    yum -y install gcc gcc-c++ autoconf nss_ldap libjpeg libjpeg-devel libpng libpng-devel freetype free ...

  10. JSON 与 JSONP

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. 即 JSON 是一种轻量级的数据交换格式. 1. ...