Farmer Robert has been running a very successful cereal farm for many years. Now he wants to diversify his business and get into growing potatoes. To this end, he has bought a new plot of land on which he plans to plant the potatoes. This field is a rectangle and is exactly ℓ metres long and w metres wide.
Since Robert is new to the potato business, he has initially purchased n different potato varieties to try out in the first year. He plans to divide his plot of land into nn parts of equal area and plant one of the varieties on each. To make it easier for him to work the fields with his tractor, each new piece of land should itself be a rectangle and have integer side lengths. Help Robert to find a suitable division of his field.
Input
The input consists of:
- One line with three integers ℓ,w,nℓ,w,n (1≤ℓ,w≤100 1≤n≤26), the length and width of Robert's field and the number of potato varieties.
Output
If there is no solution, output impossible. Otherwise output ℓ lines, each with w uppercase letters, describing a possible division of Robert's field. There should be the same number of occurrences of each of the first n letters of the English alphabet, and for each letter, its occurrences should form a single rectangular region. If there is more than one solution, any one of them will be accepted.
Examples
input
4 4 4
output
AAAA
BBCC
BBCC
DDDD
input
6 15 9
output
GGGGGBBBBBBBBBB
GGGGGAAAAAAAAAA
IIIIIIIIIIEEEEE
FFFFFFFFFFEEEEE
CCCCCDDDDDHHHHH
CCCCCDDDDDHHHHH
input
100 100 26
output
IMPOSSIBLE
题意:给定一个的矩形的高宽h,w,要求用前n个字母矩形填充该矩形,如果无解输出impossible,反之输出字母矩形。
解析:如果h*w无法整除n,那么肯定无解。因为数据比较小,我们可以暴力枚举单个矩形的长宽a,b,直到满足(a*b*n==h*w&&w%a==0&&h%b==0)就是合法矩形,然后对于每个位置py[i][j]记录相应偏移量,最后每个位置输出'A'+py[i][j]即可。
#include <stdio.h>
const int N=105;
int py[N][N];
int main()
{
int h,w,n,a,b,cnt=0;//cnt记录某个小矩形的偏移量
scanf("%d%d%d",&h,&w,&n);
if(h*w%n!=0) printf("IMPOSSIBLE\n");//无解
else{
for(a=1;a<=w;a++)//长
{
for(b=1;b<=h;b++)//宽
{
if(a*b*n==h*w&&w%a==0&&h%b==0)
{
for(int i=1;i<=h/b;i++)//行由h/b个矩形组成
{
for(int j=1;j<=w/a;j++)//列由w/a个矩形组成
{
for(int c=1;c<=b;c++)//单个矩形长
{
//单个矩形宽
for(int k=1;k<=a;k++) py[c+(i-1)*b][k+(j-1)*a]=cnt;
}
cnt++;//下一个矩形,下一个字母
}
}
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++) printf("%c",'A'+py[i][j]);
printf("\n");
}
return 0;
}
}
}
}
return 0;
}