1. 程式人生 > >zoj 3810 A Volcanic Island

zoj 3810 A Volcanic Island

四色原理模板題....

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

題意:

用 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;
}
<pre name="code" class="cpp">#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;
}