题目大意:给出一个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 - 1131C. Birthday(贪心)题目链接题目给你n和n个数,要你重新排列n个数,使得这些数的相邻差值中最大的那个值最小。stat
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
解决问题(문제해결) ACM-ICPC凭证,BOJ,CodeForces,Codewars,SCPC,摇动,TopCoder凭证
题目大意:给出 n 个数字组成的序列,现在可以对数列进行多次操作,每次操作可以选择其中一段连续的数列,用其平均数替换原位置,换句话说,若原连续数列为 1 2 3,则可以替换为 2 2 2,问如何操作可以使得最后答案...
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
构造水题了,很明显每行/列最多放一个,那么最多应该放min(n,m)+1个,由于0,0不能选,直接从左上角走一个对角线即可。 #include #define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); using ...
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
波兰球和游戏: ://codeforces.com/problemset/problem/755/B 问题779 B.怪异的舍入: : 问题845 A.国际象棋锦标赛: : 问题884 B.日语填字游戏反击: : 问题985 A.国际象棋的放置: : 问题1042 A.长凳: : 问题1105...
codeforces编程网站预测分数插件
codeForces C ++中的Code Force解决方案
暴枚最长桌脚的长度$l$,然后长度比$l$长的桌脚全部都要砍掉长度比$l$短的桌脚选择代价前$k$小的砍掉用线段树维护;示例程序 :typedef long l
Codeforces Round #620 (Div. 2) [codeforces 1304A] Two Rabbits 整除+模 总目录详见https://blog.csdn.net/mrcrack/article/details/103564004 在线测评地址https://codeforces.ml/contest/1304/problem/A ...
# sublime-plugin-for-codeforces 自定义构建,用于从 codeforces 获取测试用例并检查程序是否正确。 平台:GNU/Linux 语言:Python2.7.9 描述:这个程序从 codeforces 中获取测试用例,并告诉你程序的输出是否与...
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
Codeforces扩展包 是否曾经想让Codeforce拥有方便的快捷键,自动更新排行榜,可以按需隐藏/显示的问题标签,更好的站点导航,深色主题或以上所有功能? 这些和更多功能可以在Codeforces ++中获得! 该扩展是开源的,...
E. Array Shrinking time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an array a1,a2,…,an. You can perform the following operation...
Codeforces 185A - Plant 全测试点49个
Codeforces global round 10 codes
编码 C ++中的Codeforce问题的解决方案
Codeforces round 678 division 2 codes