UVa 1605 (构造) Building for UN

题意:

有n个国家,要设计一栋长方体的大楼,使得每个单位方格都属于其中一个国家,而且每个国家都要和其他国家相邻。

分析:

紫书上有一种很巧妙的构造方法:

一共有2层,每层n×n。一层是每行一个国家,另一层是每列一个国家。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 50;
 7 char b[2][maxn][maxn];
 8 
 9 char ToChar(int x)
10 {
11     if(x < 26) return 'a' + x;
12     else return 'A' + x - 26;
13 }
14 
15 int main()
16 {
17     int n;
18     while(scanf("%d", &n) == 1)
19     {
20         printf("2 %d %d
", n, n);
21         for(int i = 0; i < n; ++i)
22         {
23             char c = ToChar(i);
24             memset(b[0][i], c, sizeof(b[0][i]));
25             for(int j = 0; j < n; ++j) b[1][j][i] = c;
26         }
27 
28         for(int h = 0; h < 2; ++h)
29         {
30             for(int i = 0; i < n; ++i)
31             {
32                 for(int j = 0; j < n; ++j)
33                     putchar(b[h][i][j]);
34                 puts("");
35             }
36             if(!h) puts("");
37         }
38     }
39 
40     return 0;
41 }
代码君
原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4273030.html