UVA - 1605(思维题)

Building for UN UVA - 1605
The United Nations has decided to build a new headquarters in Saint Petersburg, Russia. It will have a form of a rectangular parallelepiped and will consist of several rectangular floors, one on top of another.
Each floor is a rectangular grid of the same dimensions, each cell of this grid is an office.
Two offices are considered adjacent if they are located on the same floor and share a common wall, or if one’s floor is the other’s ceiling.
The St. Petersburg building will host n national missions. Each country gets several offices that
form a connected set. Moreover, modern political situation shows that countries might want to form secret coalitions. For that to be possible, each pair of countries must have at least one pair of adjacent offices, so that they can raise the wall or the ceiling they share to perform secret pair-wise negotiations just in case they need to.You are hired to design an appropriate building for the UN.
Input
Input consists of several datasets. Each of them has a single integer number n (1 ≤ n ≤ 50) — the
number of countries that are hosted in the building.
Output
On the first line of the output for each dataset write three integer numbers h, w, and l — height, width and length of the building respectively.
h descriptions of floors should follow. Each floor description consists of l lines with w characters on each line. Separate descriptions of adjacent floors with an empty line.
Use capital and small Latin letters to denote offices of different countries. There should be at most 1 000 000 offices in the building. Each office should be occupied by a country. There should be exactly n different countries in the building. In this problem the required building design always exists.
Print a blank line between test cases.
Sample Input
4
Sample Output
2 2 2
AB
CC

zz
zz

思维题:
别被样例吓到,样例就把思路带偏了,紫书上提供了一种非常好的思路,不管n是多少,只有两层,第一层按照一行一行放完n个字母,第二层按照一列一列放完n个字母,这样构造出来的就是最终解。

ac code:

#include
using namespace std;

void print(int l){
     
	if(l<=26){
     //小于等于26就用大写字母
		printf("%c",'A'+l-1);
	}else{
     //大于26就用小写字母
		printf("%c",'a'+(l-26)-1);
	}
}

int main(){
     
	int n;
	while(~scanf("%d",&n)){
     
		char M1[n+1][n+1];
		char M2[n+1][n+1];
		
		printf("2 %d %d\n",n,n);//不管n是多少,就按照2层处理
		for(int i=1;i<=n;i++){
     
			for(int j=1;j<=n;j++){
     
				print(i);
			}
			printf("\n");
		}
		
		printf("\n");
		for(int i=1;i<=n;i++){
     
			for(int j=1;j<=n;j++){
     
				print(j);
			}
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(UVA)