2024牛客暑期多校训练营2 A Floor Tiles

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

if you need to download problem set in pdf version, click here -> 2024_Nowcoder_Multi_University_Training_Contest_2.pdf

Chino has two distinct types of floor tiles, A and B, as depicted in the figure below:


 



Chino's residence is designed as an N⋅M planar grid, and she intends to pave it entirely with these two types of tiles.

Chino is particularly fascinated by the patterns on the tiles. When the patterns on adjacent tiles align perfectly, they form a continuous curve.

 



For example, the left image in the figure above illustrates a configuration with 4 continuous curves, while the right image shows one with 5.

After having already placed the first tile, Chino is curious whether it's possible to continue the tiling in such a way that there are exactly K continuous curves. If such an arrangement exists, your task is to help her find at least one valid solution.

输入描述:

 

The first line contains a single positive integer T(1≤T≤10^{5}), — the number of test cases.

Each test case is described as follows:


The first line provides three positive integers N,M,K(1≤N,M≤800,1≤K≤2⋅N⋅M) — the dimensions of Chino's house and the desired number of continuous curves.

Subsequently, input two positive integers and a character x,y,t(1≤x≤N,1≤y≤M,t∈{′A′,′B′}) — the coordinates and type of the initially placed tile.

It is guaranteed that the sum of N⋅MN \cdot MN⋅M over all test case does not exceed 10^{6}.

输出描述:

For each test case:

If a solution exists, begin by outputting the word "Yes" to confirm its feasibility.

Then, output N⋅M characters A or B, which represents one such solution.

Otherwise, output a single line containing the word "No".

示例1

输入

2
2 4 6
1 1 A
2 4 15
1 1 A

输出

Yes
ABAB
ABAA
No

说明

 

As shown in the figure.

题目大意:(这是一道构造题)

Chino有如图所示的A和B两种不同类型的地砖,把Chino的住所设计成了一个N⋅M平面网格,她打算将其整个铺满这两种类型的瓷砖。她对瓷砖上的图案特别着迷。当相邻瓷砖上的图案完美对齐时,它们形成连续的曲线。

在已经放置第一块瓷砖后,Chino好奇是否可以继续铺砖,使得恰好有K 条连续曲线。如果存在这样的排列方式,你的任务是帮助她找到至少一个有效的解决方案。

思路:无论n,m为何值,A,B类型如何拼凑,从整个二维平面边缘进入的曲线最后一定会从边缘离开,不可能进入其内部的任何一个环内,所以我们可以得到曲线的最少总数为(n+m)。

我们可以用贪心思想将图形内环最多的情况算出来,形成环的条件就是A,B交替出现,所以我们要判断第一个出现的是A还是B,然后找规律得出:当第一个是'A'时,环最多的个数为((n-1)*(m-1)+1)/2;当第一个是'B'时,环最多的个数为(n-1)*(m-1)/2。看k是不是在这个区间内,不是的话输出"No",是的话输出"Yes",然后跟一种满足题意的情况即可(看k与最大曲线总数的差值逐一去环即可,如何去环,在代码中有解析)。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char s[805][805];
signed main()
{
	IOS
	int t;
	cin >> t;
	while(t--){
		int n,m,k;
		cin >> n >> m >> k;
		int x,y,cnt=((n-1)*(m-1)+1)/2,f=0;//cnt是环出现最多的个数,f=0表示第一个出现的是'A'
		char c; 
		cin >> x >> y >> c;//注意:(x,y)点的字母不能变 
		if(((x+y)%2==0 && c=='B') || ((x+y)%2!=0 && c=='A')) f=1,cnt=(n-1)*(m-1)/2;
		if(k<m+n || k>m+n+cnt){ 
			cout << "No" << endl;
			continue;
		}
		cout << "Yes" << endl;
		int ans=n+m+cnt-k;//ans表示要去环的个数 
		for(int i=1;i<=n;i++){//初始化 
			for(int j=1;j<=m;j++){
				if((i+j)%2==f) s[i][j]='A';
				else s[i][j]='B';
			}
		} 
		for(int i=1;i<n;i++){//开始去环 
			for(int j=2;j<=m;j++){//ans在不等于0的情况下,一旦出现"AB",必有一环 
				if(s[i][j-1]=='A' && s[i][j]=='B' && ans>0) s[i][j]=c,s[i][j-1]=c,ans--;//为什么等于c呢?因为(x,y)点的值不能变 
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cout << s[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}

  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值