[Codeforces432E]Square Tiling(贪心+构造)

题目描述

传送门
题意
将一个n*m的矩阵中的每一个方格染色,要求在保证相同颜色连在一起的方格组成一个正方形的前提下字典序最小。字典序逐行逐列比较。

题解

贪心,从上往下从左往右,能小就小。
(i,j)能够染颜色k当且仅当(i-1,j)与(i,j+1)没有染颜色k,且第i行前面的颜色k都是第一行。同时还要判断扩充这个点之后能否形成正方形。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int n,m,nowx,nowy;
int color[105][105];

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
        {
            if (color[i][j]) continue;
            for (int k=1;k<=26;++k)
            {
                if (color[i-1][j]==k||color[i][j+1]==k) continue;
                if (color[i][j-1]==k)
                {
                    if (color[i-1][j-1]==k) continue;
                    nowx=i;
                    while (nowx<n&&color[nowx+1][j-1]==k) nowx++;
                    if (nowx==n) continue;
                    for (int l=i;l<=nowx+1;++l) color[l][j]=k;
                    for (int l=j;l>=j-(nowx-i+1);--l) color[nowx+1][l]=k;
                    break;
                }
                color[i][j]=k;
                break;
            }
        }
    for (int i=1;i<=n;++i)
    {
        for (int j=1;j<=m;++j)
            putchar(color[i][j]+'A'-1);
        putchar('\n');
    }
}
发布了1157 篇原创文章 · 获赞 484 · 访问量 96万+
展开阅读全文

Holey Square Tiling

10-19

Description IELTS is around the corner! love8909 has registered for the exam, but he still hasn’t got prepared. Now he decides to take actions. But when he takes out the New Oriental IELTS Vocabulary, he finds there are so many words. But love8909 doesn’t get scared, because he has got a special skill. If he can make a list with some meaningful words, he will quickly remember these words and will not forget them. If the last letter of some word Wa is the same as the first letter of some word Wb, then you can connect these two words and make a list of two words. If you can connect a word to a list, you will make a longer list. While love8909 is making the list, he finds that some words are still meaningful words if you reverse them. For example, if you reverse the word “pat”, you will get another meaningful word “tap”. After scanning the vocabulary, love8909 has found there are N words, some of them are meaningful if reversed, while others are not. Now he wonders whether he can remember all these words using his special skill. The N-word list must contain every word once and only once. Input An integer T (T <= 50) comes on the first line, indicating the number of test cases. On the first line of each test cases is an integer N (N <= 1000), telling you that there are N words that love8909 wants to remember. Then comes N lines. Each of the following N lines has this format: word type. Word will be a string with only ‘a’~’z’, and type will be 0(not meaningful when reversed) or 1(meaningful when reversed). The length of each word is guaranteed to be less than 20. Output The format of the output is like “Case t: s”, t is the number of the test cases, starting from 1, and s is a string. For each test case, if love8909 can remember all the words, s will be “Well done!”, otherwise it’s “Poor boy!” Sample Input 3 6 aloha 0 arachnid 0 dog 0 gopher 0 tar 1 tiger 0 3 thee 1 earn 0 nothing 0 2 pat 1 acm 0 Sample Output Case 1: Well done! Case 2: Well done! Case 3: Poor boy! 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览