UVALive 6345 The Glittering Caves of Aglarond (找规律求最多)

这篇博客介绍了一个 ACM-ICPC 的问题,挑战者需要在 N x M 的钻石网格中,通过 K 次开关操作,最大化点亮的钻石数量。题目提供每个钻石当前的状态,并限制了开关操作次数。解决方案需找到最佳策略来达到最大点亮数。

The Glittering Caves of Aglarond
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

Description

Download as PDF

'Strange are the ways of Men, Legolas! Here they have one of the marvels of the Northern World, and what do they say of it? Caves, they say! Caves! Holes to fly to in time of war, to store fodder in! My good Legolas, do you know that the caverns of Helm's Deep are vast and beautiful? There would be an endless pilgrimage of Dwarves, merely to gaze at them, if such things were known to be. Aye indeed, they would pay pure gold for a brief glance!

'And, Legolas, when the torches are kindled and men walk on the sandy floors under the echoing domes, ah! then, Legolas, gems and crystals and veins of precious ore glint in the polished walls; and the light glows through folded marbles, shell-like, translucent as the living hands of Queen Galadriel.'
- Gimli, describing to Legolas the Glittering Caves of Aglarond.

While these caves are by and large natural, there is one place where the Men of Rohan have chiseled into the rock to create a magnificent exhibit. You have a wall of the cave consisting of 'lighted diamonds' arranged in a N by M grid (basically, you have a light behind each diamond which can be turned on or off). Further, you have a switch corresponding to each row of this diamond-grid. When you operate a switch, it will toggle (flip) the lights corresponding to that row.

You are given the current configuration of the lighted diamonds. Gimli challenges Legolas to turn on as many diamonds as possible using EXACTLY K on/off operations of the switches. Since Legolas is an Elf of the Wood and doesn't care much for things that glitter, he instead asks for your help. Note that the same switch (i.e. row) can be chosen multiple times.

Input

The first line contains the number of test cases T.

Each test case contains N, M and K on the first line followed by N lines containing M characters each. The i-th line denotes the state of the diamonds in the i-th row, where '*' denotes a diamond which is on and '.' denotes a diamond which is off.

Output

Output T lines containing the answer for the corresponding case.

Between successive test cases, there should not be any blank lines in the output.


Constraints:
1<=T<=100
1<=N, M<=50
1<=K<=100


Notes/Explanation of Sample Input:

In the first test case, row 1 can be toggled hence leaving all 4 lights to be in the ON state.

In the second test case, row 1 (or row 2) can be toggled twice, hence maintaining the state of the initial configuration.

Sample Input

2
2 2 1
..
**
2 2 2
..
**

Sample Output

4
2
AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
//author:XXYY
bool cmp(int a,int b)
{
	return a>b;
}
int main(){
	int t,n,m,k,i,count,j;
	int num,l1,l2,b[55],c[55];
	char a[55][55];
	scanf("%d",&t);
	while(t--){
		count=0;
		l1=l2=0;
		scanf("%d%d%d",&n,&m,&k);
		for(i=0;i<n;i++)
			scanf("%s",a[i]);
		for(i=0;i<n;i++){
			num=0;
			for(j=0;j<m;j++){
				if(a[i][j]=='*')	{
					count++;
					num++;
				}
			}
			if(m-2*num>0)
				b[l1++]=m-2*num;
			else
				c[l2++]=2*num-m;
		}
		sort(b,b+l1,cmp);
		for(i=0;i<l1;i++){
			if(k==0)
				break;
			count+=b[i];
			c[l2++]=b[i];//反转之后的.的相对于*的数目
			k--;
		}
		if(k%2)//剩余奇数步骤
        {
            sort(c,c+l2);
            count-=c[0];
        }
        printf("%d\n",count);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值