IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Codeforces Round #734 (Div. 3)-D2. Domino (hard version)-题解 -> 正文阅读

[数据结构与算法]Codeforces Round #734 (Div. 3)-D2. Domino (hard version)-题解

Codeforces Round #734 (Div. 3)-D2. Domino (hard version)

传送门
Time Limit: 1 second
Memory Limit: 256 megabytes

Problem Description

The only difference between this problem and D1 is that you don’t have to provide the way to construct the answer in D1, but you have to do it in this problem.

There’s a table of n × m n \times m n×m cells ( n n n rows and m m m columns). The value of n ? m n \cdot m n?m is even.

A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other).

You need to place n m 2 \frac{nm}{2} 2nm? dominoes on the table so that exactly k k k of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table.

Input

The first line contains one integer t t t ( 1 ≤ t ≤ 10 1 \le t \le 10 1t10) — the number of test cases. Then t t t test cases follow.

Each test case consists of a single line. The line contains three integers n n n, m m m, k k k ( 1 ≤ n , m ≤ 100 1 \le n,m \le 100 1n,m100, 0 ≤ k ≤ n m 2 0 \le k \le \frac{nm}{2} 0k2nm?, n ? m n \cdot m n?m is even) — the count of rows, columns and horizontal dominoes, respectively.

Output

For each test case:

Sample Input

8
4 4 2
2 3 0
3 2 3
1 2 0
2 4 2
5 2 2
2 17 16
2 1 1

Sample Onput

YES
accx
aegx
bega
bdda
YES
aha
aha
YES
zz
aa
zz
NO
YES
aaza
bbza
NO
YES
bbaabbaabbaabbaay
ddccddccddccddccy
NO

题目大意

D2和D1的唯一区别就是D2需要输出具体如何放置。

解题思路

既然D1已经知道如何放置了,那么D2就简单了,只需要用程序生成放置信息就行了。
根据图着色问题的四色定理,4种字母其实就够了。但是为什么要那么逼自己呢,多几种字母它不香吗?

因此采用水平的骨牌用a,b;竖直的骨牌用c,d;特殊一点的骨牌用e,f。

  • 水平骨牌用a,b,可以看这个水平骨牌是所有水平骨牌中的第几行第几列。行+列 为偶数就用a,奇数就用b。
  • 竖直的同理,行+列 为偶数就用c,奇数就用d。
  • 特殊的特殊在哪里呢,特殊在放完水平骨牌但是没把一列放满的地方,这些地方也要放竖直骨牌,但是其实位置不好确定,容易与后面的骨牌颜色相同。总之这一小部分用ef就行了。

具体如何实现可以参考一下代码。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
char c[105][105];
void prt(char c[][105], int n, int m) // 打印
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            putchar(c[i][j]);
        }
        puts("");
    }
}
void prt(char c[][105], int n, int m, bool inverse) // 重载函数,意思是旋转90°打印。
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            putchar(c[j][i]);
        }
        puts("");
    }
}
void analyse(int n, int m, int k, bool inverse) // 分析如何放置
{
    if(k%2)//奇数个水平
    {
        puts("NO"); // 具体为什么可以参考上一题解释:https://blog.csdn.net/Tisfy/article/details/119065190
    }
    else
    {
        int lie=k/n;
        lie += (k%n!=0);
        if(lie*2>m)
        {
            puts("NO"); // 不能成功放置
        }
        else
        {
            puts("YES"); // 可以放置,和上题方法一样放置
            for(int i=0;i<n;i++) // n行
            {
                for(int j=0;j<lie-1;j++)// 放置几列水平骨牌
                {
                    c[i][j*2]=c[i][j*2+1]=(i+j)%2?'b':'a';
                }
            }
            int remain=k-(lie-1)*n; // 剩下的放不满水平骨牌的那一列要放置多少行
            for(int i=0;i<remain;i++) // 对于这最后的一列一列
            {
                int j=lie-1;
                c[i][j*2]=c[i][j*2+1]=(i+j)%2?'b':'a'; // 同样的方法
            }
            // prt(c,n,m);//************
            // 开始摆放竖直的
            if(lie>0) // 这个需要加上,不然lie-1就变成-的了
            {
                int lieRemain=n-remain; // 最后一列水平放置的地方剩下多少个空行
                for(int i=0;i*2<lieRemain;i++) // 竖直骨牌每个占2行
                {
                    int j=(lie-1)*2;
                    c[i*2+remain][j]=c[i*2+1+remain][j]=(i+j)%2?'d':'c'; // 用c和d
                    c[i*2+remain][j+1]=c[i*2+1+remain][j+1]=(i+j+1)%2?'d':'c';
                }
            }
            for(int j=lie*2;j<m;j++) // 剩下的就是整列的了
            {
                for(int i=0;i*2<n;i++) // 用ef(和题解描述所用字母有所不同,但问题不大)
                {
                    c[i*2][j]=c[i*2+1][j]=(i+j)%2?'e':'f';
                }
            }
            if(inverse) // 如果需要旋转,就调用旋转打印函数
            {
                prt(c, n, m, 1);
            }
            else // 不用旋转就调用普通打印函数
            {
                prt(c, n, m);
            }
        }          
    }
}
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n,m,k;
        cin>>n>>m>>k;
        if(n%2==0)//偶数行
        {
            analyse(n,m,k,0);
        }
        else // 奇数行
        {
            swap(n,m); // 行列互换就变成偶数行了
            k=n*m/2-k; // 那么就要放置这么多个水平的(旋转之前的竖直的骨牌)
            analyse(n,m,k,1); // 分析,且是旋转过的
        }        
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119065553

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-25 11:55:27  更:2021-07-25 11:55:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 8:44:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码