Zoj 3810 a volcanic island

Source: Internet
Author: User

Tag: zoj Construction

Question:

The N * n lattice divides it into N parts connected blocks whose size is N. Each block must have different shapes. The lattice is colored in four colors and then output.

Ideas:

Purely constructed questions Test IQ, but there are still ideas to find

First of all, this question should be done by dividing the grid into different regions (I can't think of it ...) If we divide the two rows into two blocks with an area of N, we will find that they are in the same shape. Therefore, the two rows won't work, so we try 3 actions and 1 region !! Can be constructed:

In this way, we can ensure that the shapes are different, but the green will be connected !! So we can flip the graph through parity so that the Green will not be connected, because at least the green outer length is n/2, but we have three columns and one region, that is, the maximum length of the green outer is n/3

Another problem is that N is not necessarily divisible by 3. If the remaining 1 is easy to handle (directly adding a solid line), the remaining 2 is troublesome.

If the remaining 2 is used, we need to remove the first one (that is, the green side length starts from 2 during the construction). At this time, we have five rows to fill in a solid color, with four rows left. Then we construct it as follows:

According to the above constructor, a problem occurs when n is divisible by 3. Because the green part in Figure 1 may change to a rectangle, resulting in the same red and blue shapes, you only need to replace one of the red and blue grids. there are a lot of methods to change (as long as the two are asymmetrical)

Note that when determining 1 and 5, you can directly construct an answer and Output 2, 3, and 4 without an answer.

Code:

#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<set>#include<vector>using namespace std;typedef long long LL;#define N 110#define M 400010#define inf 2147483647#define lowbit(x) (x&(-x))char f[N][N]; //RBYGint main() {int t, n, i, j, k, flag;scanf("%d", &t);while (t--) {scanf("%d", &n);if (n == 1)puts("R");else if (n < 5)puts("No solution!");else if (n == 5) {puts("YYYGR");puts("YGGGR");puts("YGYYR");puts("BYYYR");puts("BBBBR");} else {flag = n / 3;for (k = n; k > 5; k -= 3, flag--) {for (i = k; i > k - 3; i--) {for (j = 1; j <= n; j++)f[i][j] = 'R';}if (flag & 1) {for (i = k; i > k - 3; i--) {for (j = n - flag + 1; j <= n; j++)f[i][j] = 'Y';}for (j = flag * 2 + 1; j <= n; j++)f[k - 1][j] = 'Y';for (j = flag + 1; j <= flag + flag; j++)f[k - 1][j] = 'B';for (j = 1; j <= n - flag; j++)f[k][j] = 'B';} else {for (i = k; i > k - 3; i--) {for (j = 1; j <= flag; j++)f[i][j] = 'Y';}for (j = 1; j <= n - flag * 2; j++)f[k - 1][j] = 'Y';for (j = n - flag - flag + 1; j <= n - flag; j++)f[k - 1][j] = 'B';for (j = flag + 1; j <= n; j++)f[k][j] = 'B';}}if (k > 3) {for (j = 1; j <= n; j++)f[k][j] = 'G';k--;}if (k > 3) {for (i = 1; i <= 4; i++) {for (j = 1; j <= n; j++)f[i][j] = 'G';}for (i = 1; i <= 4; i++)f[i][1] = 'R';for (j = 2; j <= n - 3; j++)f[4][j] = 'R';f[4][n - 2] = f[4][n - 1] = f[4][n] = 'B';for (j = 2; j <= n - 2; j++)f[3][j] = 'B';f[3][n - 1] = f[3][n] = 'Y';for (j = 2; j <= n - 1; j++)f[2][j] = 'Y';} else {for (i = 1; i <= 3; i++) {for (j = 1; j <= n; j++)f[i][j] = 'Y';}for (j = 1; j <= n - 1; j++)f[1][j] = 'R';f[2][1] = 'R';for (j = 1; j <= n - 1; j++)f[3][j] = 'B';f[2][2] = 'B';}if (n % 3 == 0) {flag = n / 3;if (flag & 1)swap(f[n - 1][1], f[n - 1][flag + 1]);elseswap(f[n - 1][n - flag], f[n - 1][n]);}for (i = 1; i <= n; i++) {for (j = 1; j <= n; j++)putchar(f[i][j]);putchar('\n');}}}return 0;}


Zoj 3810 a volcanic island

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.