题目
题意:
给定一个n*m的矩阵,矩阵上有的地方有米。你一共有k只小鸡,现在你需要分配他们的地方。要求不能同一个格子分配给两只小鸡,每个格子都要分配,且每只鸡分配到的位置是联通的,使得能吃到最多米的和最少米的差值最小。
分析:
要使得差值最小,很显然平均分那些米,余数也平均分配,最多差1。因为要联通,所以分配的时候要蛇形遍历,奇数行正序,偶数行倒序。剩下的就是模拟这个过程就好了。
#include <iostream>
using namespace std;
char a[105][105];
char res[105][105];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while( t-- )
{
int n,m,k;
cin >> n >> m >> k;
int num = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
if( a[i][j] == 'R' ) num ++;
}
}
int z1 = num / k;
int z2 = num % k;
int index = 1;
int temp;
if( z2 != 0 ) temp = z1 + 1;
else temp = z1;
char ans = 'a';
for (int i = 1; i <= n; i++)
{
if( i & 1 )
{
for (int j = 1; j <= m; j++)
{
if( a[i][j] == 'R' && temp == 0 )
{
index ++;
if( index > z2 ) temp = z1;
else temp = z1 + 1;
if( ans == 'z' ) ans = 'A';
else if( ans == 'Z' ) ans = '0';
else ans ++;
}
res[i][j] = ans;
if( a[i][j] == 'R' ) temp --;
}
}else
{
for (int j = m; j >= 1; j--)
{
if( a[i][j] == 'R' && temp == 0 )
{
index ++;
if( index > z2 ) temp = z1;
else temp = z1 + 1;
if( ans == 'z' ) ans = 'A';
else if( ans == 'Z' ) ans = '0';
else ans ++;
}
res[i][j] = ans;
if( a[i][j] == 'R' ) temp --;
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << res[i][j];
}
cout << '\n';
}
}
return 0;
}