Description
Input
Output
Sample Input
1 3
Sample Output
ABA
Data Constraint
n、m≤100
思路
贪心
枚举每一块砖,如果这个位置为空,就填入字典序最小的砖块,然后将边长一格格拓展如果当前右侧位置可以填入字典序更小的砖块,就不拓展否则判断能否拓展并拓展。
代码
#include<cstdio>
using namespace std;
char ans[102][102];
int n,m;
bool tried(int x,int y,char a)
{
if(y==1)
{
if(ans[x-1][y]!=a)
{
ans[x][y]=a; return 1;
}
else return 0;
}
else
{
if(ans[x][y-1]==a)
{
if(ans[x-1][y]==a) return 0;
int xx=x,p=1;
while(ans[xx][y-1]==a) xx++,p++;
if(xx>n||y-p+1<=0) return 0;
for(int yy=y;yy>=y-p+1;yy--) if(ans[xx][yy]>='A') return 0;
for(int yy=y;yy>=y-p+1;yy--) ans[xx][yy]=a;
for(int i=x; i<=xx; i++) ans[i][y]=a;
return 1;
}
else
{
if(ans[x-1][y]!=a&&ans[x][y+1]!=a) {ans[x][y]=a;return 1;}
else return 0;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(!ans[i][j])
for(int l='A'; l<='D'; l++) if(tried(i,j,l)) break;
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++) printf("%c",ans[i][j]);
printf("\n");
}
}