CF1898C Colorful Grid 题解
题目大意
已知有一个
现在规定一条可行路为一个点集 R
或 B
这两种颜色,使得存在一条长度为 NO
,否则输出 YES
,并输出解决方案。
解题思路
很简单的一道模拟题,容易发现,最短路径的边长度之和为 NO
即可。读者自己模拟以下就可以发现,不存在 NO
,剩余情况易证均存在一个合法的方案。
我们发现,如果走一格的上半部分,那么会多出
同样,如果绕一圈,那么会多出
那么,我们可以考虑,如果
注意事项
注意题目中要求的输出格式以及走的顺序。
AC 代码
码风较丑,不喜勿喷。
#include<math.h>
#include<time.h>
#include<stdio.h>
#include<algorithm>
#define ll long long
#define N 20
int n,m,k;
char map1[N][N];
char map2[N][N];
inline void work(){
scanf("%d%d%d",&n,&m,&k);
int mint=n-1+m-1;
if(k<mint){
puts("No");
return;
}
if((k-mint)&1){
puts("No");
return;
}puts("Yes");
int res=k-mint;
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j)
map1[i][j]=map2[i][j]='R';
int col=0;
if(res%4==0){
for(register int i=1;i<n;++i){
col=col^1;
if(col==1)
map2[i][1]='R';
else map2[i][1]='B';
}
for(register int j=1;j<m;++j){
col=col^1;
if(col==1)
map1[n][j]='R';
else map1[n][j]='B';
}
col=col^1;
if(col==1)
map2[n-1][m]='R';
else map2[n-1][m]='B';
col=col^1;
if(col==1)
map1[n-1][m-1]='R';
else map1[n-1][m-1]='B';
col=col^1;
if(col==1)
map2[n-1][m-1]='R';
else map2[n-1][m-1]='B';
}else{
for(register int i=1;i<n;++i){
col=col^1;
if(col==1)
map2[i][1]='R';
else map2[i][1]='B';
}
for(register int j=1;j<m-1;++j){
col=col^1;
if(col==1)
map1[n][j]='R';
else map1[n][j]='B';
}
col=col^1;
if(col==1)
map2[n-1][m-1]='R';
else map2[n-1][m-1]='B';
col=col^1;
if(col==1)
map1[n-1][m-1]='R';
else map1[n-1][m-1]='B';
col=col^1;
if(col==1)
map2[n-1][m]='R';
else map2[n-1][m]='B';
col=col^1;
if(col==1)
map1[n][m-1]='R';
else map1[n][m-1]='B';
}
for(register int i=1;i<=n;++i){
for(register int j=1;j<m;++j){
if(j!=1) putchar(' ');
putchar(map1[i][j]);
}putchar('\n');
}
for(register int i=1;i<n;++i){
for(register int j=1;j<=m;++j){
if(j!=1) putchar(' ');
putchar(map2[i][j]);
}putchar('\n');
}
}signed main(){
srand(114514);
srand(rand());
srand(time(0));
int T;scanf("%d",&T);
while(T--) work();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下