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 ...
随机推荐
- wpf,离线状态下部分功能不可用。
离线状态下,设置按钮的不可用.通过改变资源字典的值. App.xaml 文件下添加如下 xmlns:sys="clr-namespace:System;assembly=mscorlib&q ...
- jQuery插件(选项卡)
使用选项卡插件可以将<ul>中的<li>选项定义为选项标题,在标题中,再使用<a>元素的“href”属性设置选项标题对应的内容,它的调用格式如下: $(select ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest
A. Toda 2 按题意模拟即可. #include <bits/stdc++.h> using namespace std ; typedef pair < int , int ...
- 点 击 直 接加我QQ的功能
<a target="_blank" href="tencent://message/?uin=2814920598&Site=&Menu=yes& ...
- Django分析之如何自定义manage命令
我们都用过Django的manage.py的命令,而manage.py是在我们创建Django项目的时候就自动生成在根目录下的一个命令行工具,它可以执行一些简单的命令,其功能是将Django proj ...
- Redis 配置文件总结
1.1 文件说明 1.1.1 主要文件说明 1 redis-server Redis服务器 2 redis-cli Redis命令行客户端 3 redis-benchemark redis-be ...
- CSV格式数据如何导入SqlServer?
一.使用微软数据库IDE管理软件:Microsoft SQL Server Management Studio 1.标准的CSV文件格式如下: 2.建数据表 3.在需要导入的数据库右键点击“任务”,选 ...
- Ubuntu不显示壁纸,桌面右键无反应解决
用ubuntu tweak调整ubuntu的桌面图标显示,导致桌面无法显示壁纸,桌面点击右键无发应。 解决办法:Ubuntu Tweak中“调整”选项卡-》”显示桌面图标“的选项一定要打开,处于ON状 ...
- 移动端/H5关于cursor:pointer导致的问题
cursor属性规定要显示的光标的类型(形状),该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状(不过 CSS2.1 没有定义由哪个边界确定这个范围). 不过,这个属性用在PC端没有任何问题 ...
- 键盘和alertView的冲突问题
冲突现象: textField失去响应收键盘时,如果要弹出一个alertView警告框,就会出现一个现象:在点击了alertView上的事件后,在alertView消失后,会有短暂的出现键盘会再次弹出 ...