UVa 1605 (构造) Building for UN

本文介绍了一种紫书上的巧妙构造方法,用于设计长方体大楼,确保每个单位方格属于一个国家,并且每个国家与其他国家相邻。通过两层布局,每层n×n,一层按照行排列国家,另一层按照列排列国家,实现国家间的直接相邻。

题意:

有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, 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值