## D. Feeding Chicken(Codeforces Round #601 (Div. 2))(思维)

D. Feeding Chicken(Codeforces Round #601 (Div. 2))(思维)

题目链接
在这里插入图片描述

inputCopy

4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

outputCopy

11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789

Note

These pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.

In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 0 0 .

在这里插入图片描述

In the second test case, there are 4 4 chickens with 3 3 cells of rice, and 2 2 chickens with 2 2 cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is 3 2 = 1 3−2=1 .

在这里插入图片描述

In the third test case, each chicken has 3 3 cells with rice.
在这里插入图片描述
In the last test case, since there are 62 62 chicken with exactly 62 62 cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.

题意

这里有一片田地,有的格子上有大米,你必须尽可能地均匀地分配这些有大米的玉米地给 n n 只鸡,而且分给同一只鸡的玉米地必须是一个连通块。

按照S形枚举,把路径上的地分给同一只鸡即可

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 100
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

char a[maxn][maxn];
int n, m, k;
int sum;
char b[maxn][maxn];
char cc[62];

void init() {
	mem(b, 0);
	sum = 0;
	_for(i, 10) cc[i] = '0' + i;
	_for(i, 26) cc[10 + i] = 'a' + i;
	_for(i, 26) cc[36 + i] = 'A' + i;
}

void sol() {
	init();
	cin >> n >> m >> k;
	_for(i, n) cin >> a[i];
	_for(i, n) {
		_for(j, m) {
			if (a[i][j] == 'R') {
				sum++;
			}
		}
	}
	int tem = 0, cur = 0;
	_for(i, n) {
		if (i & 1) {
			_for(j, m) {
				if ((a[i][j] == 'R') + cur > (tem < sum % k ? sum / k + 1 : sum / k)) {
					tem++;
					cur = 0;
				}
				if (a[i][j] == 'R') cur++;
				b[i][j] = cc[tem];
			}
		}
		else {
			for (int j = m - 1; j >= 0; j--) {
				if ((a[i][j] == 'R') + cur > (tem < sum % k ? sum / k + 1 : sum / k)) {
					tem++;
					cur = 0;
				}
				if (a[i][j] == 'R') cur++;
				b[i][j] = cc[tem];
			}
		}
	}
	_for(i, n) {
		_for(j, m) {
			cout << b[i][j];
		}
		cout << "\n";
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);

	int T;
	cin >> T;
	_for(i, T) {
		sol();
	}
	return 0;
}
发布了163 篇原创文章 · 获赞 54 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42856843/article/details/103222908