Problem Statement

    

There is a H times W rectangle divided into unit cells. The rows of cells are numbered 0 to H-1 from top to bottom, and the columns are numbered 0 to W-1 from left to right. The corners of cells are called lattice points. By definition, there are (H+1)*(W+1) lattice points in our rectangle.

Each of the four edges of each cell is painted white. Additionally, in some cells exactly one of the two diagonals is painted white. In the remaining cells no diagonal is painted white yet. Later, you are going to paint exactly one of the diagonals white in each of these cells.

Once you are done painting the diagonals, your next goal will be to color each of the lattice points using one of three available colors: red, green, or blue. There is only one constraint: adjacent lattice points are not allowed to share the same color.

Two lattice points are called adjacent if they are connected by a white line segment. (In other words, consecutive corners of a cell are always adjacent, opposite corners of a cell are adjacent if and only if they are connected by a painted diagonal, and no other pairs of lattice points are adjacent.)

Obviously, you need to paint the missing diagonals in such a way that there will be a valid coloring of lattice points, if possible.

You are given a vector <string> cells with H elements, each consisting of W characters. If cells[i][j] is 'N', there is a diagonal line from the top left to the bottom right corner in the cell in row i, column j. If cells[i][j] is 'Z', there is a diagonal line from the top right to the bottom left corner in the cell in row i, column j. If cells[i][j] is '?', there is no diagonal yet in the cell in row i, column j.

If it is impossible to fill in the missing diagonals in such a way that there will be a valid coloring of all lattice points, return an empty vector <string>. Otherwise, return a vector <string> that represents the rectangle with all the missing diagonals filled in. I.e., the return value must be obtained from cells by replacing each '?' by either 'N' or 'Z'. The return value must represent a rectangle for which a valid coloring of its lattice points exists. If there are multiple possibilities, return the lexicographically smallest one.

Definition

    
Class: ThreeColorability
Method: lexSmallest
Parameters: vector <string>
Returns: vector <string>
Method signature: vector <string> lexSmallest(vector <string> cells)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Notes

- Given two different vector <string>s A and B with the same number of elements, find the smallest index i such that A[i] and B[i] differ. If A[i] < B[i] we say that A is lexicographically smaller than B and vice versa.

Constraints

- cells will contain between 1 and 50 elements, inclusive.
- Each element of cells will contain between 1 and 50 characters, inclusive.
- All elements of cells will contain the same number of characters.
- Each character of cells will be either 'N' or 'Z' or '?'.

Examples

0)  
    
{"Z"}
Returns: {"Z" }
The given rectangle and a possible coloring is as follows.

1)  
    
{"??", "?N"}
Returns: {"NN", "NN" }

2)  
    
{"ZZZ", "ZNZ"}
Returns: { }
 
3)  
    
{"N?N??NN","??ZN??Z","NN???Z?","ZZZ?Z??","Z???NN?","N?????N","ZZ?N?NN"}
Returns: { }
 
4)  
    
{"ZZZZ","ZZZZ","ZZZZ"}
Returns: {"ZZZZ", "ZZZZ", "ZZZZ" }
 

题意:给定一个网格图,为每个格子安排对角线(有些格子已经安排好),使得新图可以被用三种颜色染色(被一条边相连的两个点不能染同一种颜色)。两种对角线用'Z'与'N'表示。

题解:

画图找规律,可以发现,在相邻的四个格子中(即3*3的格点),'Z'的数量为偶数。

由此可以推得,在一个合法的图中,一行格子的对角线安排如果不与相邻的行完全相同,就与相邻的行完全相反。

即不管在哪一行,第i列与第j列格子对角线安排是否相同的关系都是一样的。

即我们可以用已知的每一行内的关系,通过并查集处理出第一行各格子的关系。

然后在满足原本安排方案的情况下,求出一种字典序最小的安排方案。

代码:

 int fa[],a[][],n,m;
int getf(int x)
{
if(fa[x]!=x)fa[x]=getf(fa[x]);
return fa[x];
}
int hb(int xx,int yy)
{
int x=getf(xx),y=getf(xx+m),z=getf(yy),w=getf(yy+m);
if((x==w)or(y==z))return ;
fa[x]=z; fa[y]=w; return ;
}
int qf(int xx,int yy)
{
int x=getf(xx),y=getf(xx+m),z=getf(yy),w=getf(yy+m);
if((x==z)or(y==w))return ;
fa[x]=w; fa[y]=z; return ;
}
int pd(char x)
{ if(x=='N')return ; if(x=='Z')return -; return ; }
class ThreeColorability
{
public:
vector <string> lexSmallest(vector <string> cells)
{
//$CARETPOSITION$
vector<string>ans;
n=cells.size(); m=cells[].size();
for(int i=;i<m;i++)fa[i]=i,fa[i+m]=i+m;
for(int i=;i<n;i++)
for(int j=;j<m-;j++)
if(cells[i][j]!='?')
{
for(int k=j+;k<m;k++)
if(cells[i][k]!='?')
{
int flag;
if(cells[i][j]==cells[i][k])flag=hb(j,k);else flag=qf(j,k);
if(flag==)return ans;
}
}
for(int j=;j<m;j++)
{
int t=pd(cells[][j]);
if(t!=)
{
int x=getf(j),y=getf(j+m);
for(int k=;k<m;k++)
{
if(getf(k)==x)a[][k]=t;else
if(getf(k)==y)a[][k]=-t;
}
}
}
for(int j=;j<m;j++)
if(a[][j]==)
{
int x=getf(j),y=getf(j+m);
for(int k=;k<m;k++)
{
if(getf(k)==x)a[][k]=;else
if(getf(k)==y)a[][k]=-;
}
}
for(int i=;i<n;i++)
{
int x=;
for(int j=;j<m;j++)if(pd(cells[i][j])!=)
{ if(pd(cells[i][j])==a[i-][j])x=;else x=-; break; }
if(x==){ if(a[i-][]==)x=;else x=-; }
for(int j=;j<m;j++)a[i][j]=a[i-][j]*x;
}
for(int i=;i<n;i++)
{
string s; s.clear();
for(int j=;j<m;j++)if(a[i][j]==)s=s+'N';else s=s+'Z';
ans.push_back(s);
}
return ans;
}
};

TopCoder[SRM587 DIV 1]:ThreeColorability(900)的更多相关文章

  1. TopCoder[SRM587 DIV 1]:TriangleXor(550)

    Problem Statement      You are given an int W. There is a rectangle in the XY-plane with corners at ...

  2. TopCoder SRM 559 Div 1 - Problem 900 CircusTents

    传送门:https://284914869.github.io/AEoj/559.html 题目简述: n个实心圆,两两没有交集,在第一个圆上找一个点,使得它到另外一个圆上某个点的最短距离的最小值尽量 ...

  3. TopCoder[SRM513 DIV 1]:Reflections(1000)

    Problem Statement      Manao is playing a new game called Reflections. The goal of the game is trans ...

  4. Topcoder SRM584 DIV 2 500

    #include <set> #include <iostream> #include <string> #include <vector> using ...

  5. Topcoder SRM583 DIV 2 250

    #include <string> #include <iostream> using namespace std; class SwappingDigits { public ...

  6. 【补解体报告】topcoder 634 DIV 2

    A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...

  7. Topcoder Srm627 DIV 2

    A,B:很水,注意边界,话说HACK都是这些原因. C: R[I][J]:表示反转I-J能改变冒泡排序的次数: DP方程:dp[i][k]=max(dp[j][k],dp[j][k-1]+dp[j][ ...

  8. Topcoder SRM548 Div 1

    1. KingdomAndTrees 给出n个数a[1..n],求一个数组b[1..n]满足b严格递增,且b[1]>=1. 定义代价为W = max{abs(a[i]-b[i])},求代价最小值 ...

  9. TopCoder SRM 701 Div2 Problem 900 ThueMorseGame(博弈+预处理)

    题意  Alice和Bob在玩一个游戏,Alice先手. 每次一个人可以从一堆式子中拿走任意数量(不超过m)的式子. 取走最后一颗式子的人胜利. 当一个取完某一步的时候剩下的石子数量的二进制表示中1的 ...

随机推荐

  1. 代理端口转发工具rinetd

    转载: https://my.oschina.net/wuweixiang/blog/2983280 rinetd 前言 iptables 的功能当然强大,但理解与设置却有点抽象,便通过google认 ...

  2. png图标任意赋色

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Delphi 消息函数 SendMessage函数和 PostMessage的区别

    SendMessage函数 将指定的消息发到窗口.它调用特定窗口的窗口处理函数,并且不会立即返回,直到窗口处理函数处理了这个消息. PostMessage函数 将一个消息放入与创建这个窗口的消息队列相 ...

  4. Sqlserver复杂查询

    --联表修改 update xyzrb set xyzrb.xy_card=tablsb.card from xyzrb left join tablsb on xyzrb.xybh=tablsb.x ...

  5. [CQOI2014]数三角形 题解(找规律乱搞)

    题面 其实这道题不用组合数!不用容斥! 只需要一个gcd和无脑找规律(滑稽 乍一看题目,如果单纯求合法三角形的话情况太多太复杂,我们可以从局部入手,最终扩展到整体. 首先考虑这样的情况: 类似地,我们 ...

  6. [HNOI2015]菜肴制作 题解(贪心+拓扑)

    Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1到N的顺序编号,预估质量最高的菜肴编号 ...

  7. 51nod-1204 并查集

    你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案 ...

  8. XSS漏洞的渗透利用另类玩法

    XSS漏洞的渗透利用另类玩法 2017-08-08 18:20程序设计/微软/手机 作者:色豹 i春秋社区 今天就来讲一下大家都熟悉的 xss漏洞的渗透利用.相信大家对xss已经很熟悉了,但是很多安全 ...

  9. python 生成json格式文件,并存储到手机上

    上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- import json import os import random "" ...

  10. 10、TestNG 的 FixTrue用法一

    Fixture 是指一个测试运行所需的固定环境,通俗来说 ,为测试用例所准备的环境. 以下是TestNG中可用的注释及其属性的简要概述. 我们先演示@BeforeClass.@AfterClass.@ ...