ZOJ 3810 A Volcanic Island(四色定理模板)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5342

A Volcanic Island


Time Limit: 2 Seconds     
Memory Limit: 65536 KB      Special Judge



An underwater volcano has erupted massively in somewhere of the deep Atlantis Ocean. This large eruption led to the birth of a new volcanic island, which had a shape of square. Near the island, there are
N countries. All of them have claimed the sovereignty over the island.

After a lot of multilateral negotiation and occasional armed conflicts, the
N
countries decided to divide the square volcanic island equally. They partitioned the island into
N x N small equal-sized square chunks. Each country could get a connected region consists of exact
N chunks.

Two chunks A and B are called "connected" if they share an edge, or there exists another chunk C connected with both A and B. A group of chunks are called "connected region" if any two of these chunks are connected.

Every country want a unique region. It means the N regions should be different with each other. Two regions are considered as the same if and only if one can transform into the other by an isometry (a combination of rigid motions, including translation,
rotation and reflection).

In a nutshell, your task is to divide a square island with N x
N
chunks into N connected regions with different shape. You also need to draw a map to color the regions of the map so that no two edge-adjacent regions have the same color. Most of the people in these countries believed that four different
colors are enough. So you can mark these regions with at most four colors, red, green, blue and yellow.

Input

There are multiple test cases. The first line of input contains an integer
T
indicating the number of test cases. For each test case:

There is only an integer N (1 <= N <= 100).

Output

For each test case, output a valid map described above. If there is no solution, output "No solution!" instead. Please note that only four colors (‘R‘, ‘G‘, ‘B‘ and ‘Y‘) can be used to drawing the map.

Sample Input

2
2
5

Sample Output

No solution!
YYYGR
YGGGR
YGYYR
BYYYR
BBBBR

Author: ZHOU, Yuchen

Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

题意:

用 n 块面积为 n 的图块拼满一个 n*n 的矩阵图,并且 n 块图只有四种颜色,相邻的图块不能有相同的颜色,任意的两块不能有相同的形状,包括旋转,对称;

注意当n为6 的时候会有相同的块,所以要特判一下;

代码如下:

#include <cstdio>
#include <cstring>

const int MAXN = 117;
char mm[MAXN][MAXN];
char col[2];
int n;

void solve()
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            mm[i][j] = 'B';
        }
    }
    for(int i = 0; i < n; i++)
        mm[0][i] = 'Y';
    for(int i = 0; i < (n-1)/2; i++)
    {
        char c = col[i%2];
        for(int j = i+1; j < n; j++)
        {
            mm[j][i] = c;
        }
        for(int j = 1; j <= i+1; j++)
        {
            mm[j][i+1] = c;
        }
    }
    for(int i = (n-1)/2; i < n-1; i++)
    {
        char c = col[i%2];
        for(int j = i+2; j < n; j++)
        {
            mm[j][i] = c;
        }
        mm[i+2][i+1] = c;
        for(int j = 2; j <= i+2; j++)
        {
            mm[j][i+2] = c;
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            printf("%c",mm[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    col[0] = 'G';
    col[1] = 'R';
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n == 1)
            printf("Y\n");
        else if(n <= 4)
            printf("No solution!\n");
        else if(n == 6)//特判
        {
            printf("YYYYYY\n");
            printf("GGRGRR\n");
            printf("GRRGRB\n");
            printf("GRGGRB\n");
            printf("GRGRRB\n");
            printf("GRGBBB\n");
        }
        else
        {
            solve();
        }
    }
    return 0;
}
时间: 2024-10-04 05:17:19

ZOJ 3810 A Volcanic Island(四色定理模板)的相关文章

ZOJ 3810 - A Volcanic Island ( 构造 )

ZOJ 3810 - A Volcanic Island ( 构造 ) 题意: 给定一个N*N 的方格,需要用4种颜色进行染色, 要求:划分出N片区域,每片区域用一种颜色,且构造出的区域形状,颜色,旋转后的形状都不能相同 分析: 构造的题目一直都不是很好做,主要是因为自己智商太低.. 这个是看了郏老大的题解才会构造的,至于为什么这样构造.也说不出一个所以然来. 代码: #include <cstdio> #include <cstring> #include <algorit

zoj 3810 A Volcanic Island(构造)

题目链接:zoj 3810 A Volcanic Island 题目大意:给定n,要求用n块面积为n的拼图铺满n?n的矩阵,任意两块拼图形状不能相同(包括旋转和镜像),并且n块拼图只能有4中颜色,相邻两块拼图颜色不能相同. 解题思路:构造,n = 2,3,4时是不存在的.然后对于n >= 5的直接构造,具体看代码.注意这种构造方式构造6的时候会出现相同的拼图,所以特判. #include <cstdio> #include <cstring> #include <alg

ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)

1.题目描写叙述:点击打开链接 2.解题思路:本题是四色定理的模板题.只是有几种情况要提前特判一下:n==1直接输出,1<n<5时候无解,n==6时候套用模板会出现同样的块.因此要特判一下.其它情况都能直接利用模板构造出来. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<cassert> #include<string>

ZOJ 3810 A Volcanic Island

题意: n*n的格子  将它分成n份大小为n的连通块  要求每个块形状不同  用4种颜色将格子染色后输出 思路: 纯构造题  考验智商  不过还是有思路可寻的 首先这题要想到将格子分区域的去做(想不到就没办法了-)假设我们两行为一个区域  那么将这两行划分成两个面积为n的块之后发现它们的形状必然相同  因此两行不行  所以我们尝试3行为1个区域!!  可以构造: 这样构造就可以保证形状不同  但是绿色会连上!!  那么我们通过奇偶性将图翻转  这样绿色就不会连上了  因为连起来至少需要绿色的外边

ZOJ 3810 A Volcanic Island 构造题

题目链接:点击打开链接 学弟是厉害啊.. #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> using namespace std; int vis[110][110],a[110][110],n; void fuck1() { for(int ii=1;ii<=n/2;ii++)//7 { int i

ZOJ 1586 QS Network prim优化模板

链接: 1586 题意: 有一个N X N的网络,每两个点都有边相连,边的权值用邻接矩阵输入,每个点也有一个权值,当它们之间的那条边被选取时,需要加上两个点的权值.求这个网络的最小生成树. 直接套用prim算法的模板 其中用到一个节约内存的优化 将lowdistance 和visit 两个数组 结合起来 如果访问过lowdistance改成-1即可 另外该题中边的权值应为边本身的权值加上两端点的权值. #include<iostream> #include<cstdio> #inc

ZOJ 2859 二维RMQ(模板)

这题求范围最小值,RMQ正好是用来解决这方面的.所以再适合只是了,又是离线静态输入输出的,所以时间比二维线段树快. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<set> #include<cmath> #include<bit

ZOJ 3810 Pretty Poem

Pretty Poem Time Limit: 2 Seconds     Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can ev

托福分类词汇

托福分类词汇表(共17类863个常用单词) 一.   psychology 心理 1.       mental 心理的 2.       physical 身体的,物质的,物理的 3.       spiritual 心灵的 4.       conformity 从众 5.       majority 多数人 6.       minority 少数人 7.       threshold judgment (心理学)初始性判断 8.       subject 受实验对象 9.