codeforces 432E Square Tiling(构造)

E. Square Tiling
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have an n × m rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally:

  • each cell must be painted some color (the colors are marked by uppercase Latin letters);
  • we will assume that two cells of the table are connected if they are of the same color and share a side; each connected region of the table must form a square.

Given n and m, find lexicographically minimum coloring of the table that meets the described properties.

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 100).

Output

Print lexicographically minimum coloring of the table that meets the described conditions.

One coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if:

  • consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then the second cell in the first row and so on);
  • let's find in this order the first cell that has distinct colors in two colorings;
  • the letter that marks the color of the cell in X, goes alphabetically before the letter that marks the color of the cell in Y.
Examples
input
1 3
output
ABA
input
2 2
output
AA
AA
input
3 4
output
AAAB
AAAC
AAAB


题目大意:用26个大写字母将矩阵填满,要求挨在一起的相同字母必须构成一个正方形,要求构成的矩阵的字典序最小(从左往右,从上到下)

题解:从上往下从前往后,能小就小。
(i,j)能够染颜色k当且仅当(i-1,j)与(i,j+1)没有染颜色k,且第i行前面的颜色k都是第一行,那么就将正方形都扩大一圈

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 103
using namespace std;
int n,m;
int a[N][N];
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	 for (int j=1;j<=m;j++)
	 {
	 	if (a[i][j]) continue;
	 	for (int k=1;k<=26;k++)
	 	{
	 		if (a[i-1][j]==k||a[i][j+1]==k)  continue;
	 		if (a[i][j-1]==k||j==1){
	 			int num=0; int l=i;
	 			while (l<=n&&a[l][j-1]==k) num++,l++;
	 			num++;
	 			if (i+num-1>n) continue;
	 			for (int t=i;t<=i+num-1;t++) a[t][j]=k;
	 			for (int t=j;t>=j-num+1;t--) a[i+num-1][t]=k;
	 		}
	 		a[i][j]=k;
	 		break;
	 	}
	 }
    //for (int i=1;i<=m;i++)
     //cout<<a[1][i]<<" ";
    //cout<<endl;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		 printf("%c",a[i][j]+64);
		printf("\n");
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值