题目连接:Codeforces 432E Square Tiling

题目大意:给出一个n∗m的矩阵,要求对该矩阵进行上色,用大写字母,可是每次上色的区域必须是正方形,不求相邻的上色区域不能有同样的颜色。求字典序最小的方案(字典序比較。从左至右。从上到下)

解题思路:用贪心的思想去构造矩阵,由于字典序的优先级为左至右,以及上到下,所以我们每次对于一个未上色点x,y。考虑最少要放到的长度p就可以。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 105;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int n, m, g[N][N]; void draw(int x, int y, int k, int sign) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++)
g[x+i][y+j] = sign;
}
} int get(int x, int y) {
int v[15];
memset(v, 0, sizeof(v)); for (int i = 0; i < 4; i++) {
int p = x + dir[i][0];
int q = y + dir[i][1];
v[g[p][q]] = 1;
} for (int i = 1; i <= 10; i++)
if (v[i] == 0)
return i;
return 0;
} void solve (int x, int y) { if (y > m) {
y = 1;
x++;
} if (x > n)
return ; if (g[x][y]) {
solve(x, y+1);
return;
} int sign = get(x, y); int p = 1;
while (true) {
if (p + x > n || p + y > m)
break; if (g[x][y+p])
break;
int tmp = get(x, y+p); if (tmp != sign)
break;
p++;
} draw(x, y, p, sign);
solve(x, y + p);
} int main () {
scanf("%d%d", &n, &m);
memset(g, 0, sizeof(g));
solve (1, 1); for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%c", 'A'+g[i][j]-1);
printf("\n");
}
return 0;
}

Codeforces 432E Square Tiling(结构体+贪婪)的更多相关文章

  1. codeforces 432E Square Tiling

    codeforces 432E Square Tiling 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define fi ...

  2. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)

    题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...

  3. Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)

    题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色. 题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是 ...

  4. Swift----函数 、 闭包 、 枚举 、 类和结构体 、 属性

    1 数组排序 1.1 问题 本案例实现一个整型数组排序的函数,数组排序的规则由传递的规则函数决定. 1.2 方案 首先定义一个整型数组排序函数sortInts,该函数有一个整型数组类型的参数,该参数必 ...

  5. FFmpeg源代码简单分析:结构体成员管理系统-AVOption

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  6. FFMPEG结构体分析:AVCodecContext

    注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrame FFMPEG结构体分析:AVFormatContext FFMPEG结构体分析:AVCodecConte ...

  7. FFmpeg 结构体学习(六): AVCodecContext 分析

    在上文FFmpeg 结构体学习(五): AVCodec 分析我们学习了AVCodec结构体的相关内容.本文,我们将讲述一下AVCodecContext. AVCodecContext是包含变量较多的结 ...

  8. Golang面向对象编程-struct(结构体)

    Golang面向对象编程-struct(结构体) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是面向对象编程 面向对象编程(Object Oriented Program ...

  9. C#学习笔记(七):结构体、数组、冒泡排序和调试

    结构体 结构体不能重写默认无参构造函数 一位数组 using System; using System.Collections.Generic; using System.Linq; using Sy ...

随机推荐

  1. hdu1569find the safest road(floyd变形求最大安全值)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. 手把手教你_android自己主动化实践方案选型

    接到一个android自己主动化的任务,看了看手中的家伙:ranorex,appium,uiautomator 当然先捡商用的试试,简单呀,能够录制回放,只是不是抱特别大的期望,这个爷比較娇气,要是a ...

  3. STL 之 queue、priority_queue 源代码剖析

    /* * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute an ...

  4. Oracle 验证IOT表数据存储在主键里

    iot表测试: 在create table语句后面使用organization index,就指定数据表创建结构是IOT.但是在不指定主键Primary Key的情况下,是不允许建表的. create ...

  5. poj2299--B - Ultra-QuickSort(线段树,离散化)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 41215   Accepted: 14915 ...

  6. POJ3071-Football(概率DP+滚动数组)

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Descript ...

  7. 建立地方Jekyll周边环境

    近期使用github建立一个博客,只是要了解markdown语法,因为markdown后写的不是立即可见.所以,每一个成品都要经过在线调试,在线调试已经上线的文章,每次上线有反复git add, gi ...

  8. opencv 训练自己的分类器汇总

    原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...

  9. 从零开始,使用python快速开发web站点(2)

    书接上文.http://blog.csdn.net/i7788/article/details/10306595 首先是数据库的搭建,这里的django的数据模型搭建十分easy. no sql.ju ...

  10. [poj 2991]Crane[线段树表示向量之和,而非数量]

    题意: 起重机的机械臂, 由n段组成, 对某一些连接点进行旋转, 询问每次操作后的末端坐标. 思路: 由于旋转会影响到该点之后所有线段的角度, 因此容易想到用线段树记录角度, 成段更新. (但是不是每 ...