CF1898C Colorful Grid(构造)

2023-12-10 14:52
文章标签 colorful 构造 grid cf1898c

本文主要是介绍CF1898C Colorful Grid(构造),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接

题目大意

n 行 m 列 的一个矩阵,每行有m - 1条边,每列有 n - 1 条边。
问一共走 k 条边,能不能从 (1, 1),走到(n, m),要求该路径上,每条边的颜色都是红蓝交替的,可以走重复的边。
输出YES/NO

思路

  • NO的情况

    • 从起点到终点至少要走 n - 1 + m - 1步,若 k < n - 1 + m - 1, 则输出NO
    • 因为每绕一次路,都只能多走偶数步在这里插入图片描述
      所以k - (n - 1 + m - 1),是奇数时,NO
  • 构造方法

    • 因为要红蓝交替,所以不能走回头路
    • 若k == n - 1 + m - 1,直接最短路弄成红蓝交替
    • 若k > n - 1 + m - 1,res = k - (n - 1 + m - 1), res一定是偶数,偶数除以4,要不就能整除,要不就余2
      • 能被整除,就让它,绕着最后一圈转
        在这里插入图片描述

      • 若余2,则先把这两步在开头时消耗掉,剩下的,绕着最后一圈转

      • 在这里插入图片描述

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 20;
char heng[N][N];
char shu[N][N];
void init()
{for (int i = 1; i < N; i ++ ){for (int j = 1; j < N; j ++ ){if (i % 2) shu[i][j] = 'R';else{shu[i][j] = 'B';}if (j % 2 == 0){heng[i][j] = 'R';}else{heng[i][j] = 'B';}}}
}
int main()
{int T; cin >> T;while (T -- ){init();int n, m, k;cin >> n >> m >> k;int res = n - 1 + m - 1;if (res % 2 != k % 2 || k < res){cout << "NO" << endl;continue;}if (heng[1][m - 1] == 'R'){shu[1][m] = 'B';}else{shu[1][m] = 'R';}for (int i = 2; i < n; i ++ ){if (shu[i - 1][m] == 'R'){shu[i][m] = 'B';}else{shu[i][m] = 'R';}}cout << "YES" << endl;int x = (k - res) % 4;if (x == 0){if (shu[n - 1][m] == 'B'){shu[n - 1][m - 1] = 'B';heng[n][m - 1] = 'R';heng[n - 1][m - 1] = 'R';}if (shu[n - 1][m] == 'R'){shu[n - 1][m - 1] = 'R';heng[n][m - 1] = 'B';heng[n - 1][m - 1] = 'B';}}if (x == 2){if (shu[n - 1][m] == 'B'){shu[n - 1][m - 1] = 'B';heng[n][m - 1] = 'R';heng[n - 1][m - 1] = 'R';}if (shu[n - 1][m] == 'R'){shu[n - 1][m - 1] = 'R';heng[n][m - 1] = 'B';heng[n - 1][m - 1] = 'B';}if (heng[1][2] == 'B'){shu[1][1] = 'R'; shu[1][2] = 'R'; heng[2][1] = 'B';}if (heng[1][2] == 'R'){shu[1][1] = 'B'; shu[1][2] = 'B'; heng[2][1] = 'R';}}for (int i = 1; i <= n; i ++ ){for (int j = 1; j < m; j ++ ){cout << heng[i][j] << " " ;}cout << endl;}for (int i = 1; i < n; i ++ ){for (int j = 1; j <= m; j ++ ){cout << shu[i][j] << " " ;}cout << endl;}}return 0;
}

这篇关于CF1898C Colorful Grid(构造)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/477415

相关文章

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

LibSVM学习(六)——easy.py和grid.py的使用

我们在“LibSVM学习(一)”中,讲到libSVM有一个tools文件夹,里面包含有四个python文件,是用来对参数优选的。其中,常用到的是easy.py和grid.py两个文件。其实,网上也有相应的说明,但很不系统,下面结合本人的经验,对使用方法做个说明。        这两个文件都要用python(可以在http://www.python.org上下载到,需要安装)和绘图工具gnup

C++中类的构造函数调用顺序

当建立一个对象时,首先调用基类的构造函数,然后调用下一个派生类的 构造函数,依次类推,直至到达派生类次数最多的派生次数最多的类的构造函数为止。 简而言之,对象是由“底层向上”开始构造的。因为,构造函数一开始构造时,总是 要调用它的基类的构造函数,然后才开始执行其构造函数体,调用直接基类构造函数时, 如果无专门说明,就调用直接基类的默认构造函数。在对象析构时,其顺序正好相反。

Java构造和解析Json数据的两种方法(json-lib构造和解析Json数据, org.json构造和解析Json数据)

在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别。下面首先介绍用json-lib构造和解析Json数据的方法示例。 一、介绍       JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包,主要就是用来解析Json

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There

MemSQL Start[c]UP 2.0 - Round 1A(构造)

题目链接:http://codeforces.com/problemset/problem/452/A 解题思路: 打个表暴力查找匹配。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <strin

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co

Codeforces Round #233 (Div. 2)A(构造)

题目链接:http://codeforces.com/contest/399/problem/A 解题思路: 构造出来即可,考虑p-k和p+k两个边界分别于1和n作比较,对左右符号特殊处理。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include

Codeforces Round #247 (Div. 2)A(构造)

题目链接:http://codeforces.com/contest/431/problem/A 解题思路: 构造出来即可。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <string>#inc

C函数特性:构造与析构(constructor destructor)

文章目录 0x1 constructor0x2 constructor_priority0x3 destructor0x4 destructor_priority 0x1 constructor attribute((constructor)) 是 GCC 编译器的一个特性,它允许定义一个特殊的函数,这个函数会在 main 函数执行之前,也就是程序开始执行时被调用。 这通常用于执