codeforces 432E Square Tiling(构造)
2016-05-27 19:12
323 查看
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
output
input
output
input
output
题目大意:用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
;
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");
}
}
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
;
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");
}
}
相关文章推荐
- 莫比乌斯反演—详解
- hibernate缓存:一级缓存和二级缓存
- EventBus遇到的BUG
- CityMaker学习教程05 模型导入
- ubuntu14.04设置静态ip
- java生成doc和jar
- android SwipeRefreshLayout使用遇到问题分(一)--布局
- php7 宏杂记
- Linux探秘之I/O效率
- 软件设计模式、目标、原则
- 修改jsonb的属性
- js动画 从一个位置渐渐移动到另外一个位置 通用
- 小马哥----高仿苹果6sp主板型号A9910-尾插盖板A992标示 刷机拆机图与开机界面图
- 并发服务器
- hdu1498 50 years, 50 colors(最小顶点覆盖)
- Qt之自定义布局管理器(QBorderLayout)
- Qt之自定义布局管理器(QBorderLayout)
- Android客户端缓存机制(文字缓存和多媒体文件缓存)
- 闰年和平年
- sdutoj 3275第七届校赛--LCM的个数