import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int m=scanner.nextInt();
int k=scanner.nextInt();
char a[][]=new char[n+5][m];
for (int i = 0; i < n; i++) {
String string=scanner.next();
a[i]=string.toCharArray();
}
int count=0;
// 这道题核心思想是贪心
// 我们要去找性价比最高的路
// 其实就是在列的方向上找尽可能长的路
// 我们可以把每一条符合条件的路的长度都保存起来
// 然后根据长度进行从大到小排序
int l=0;
ArrayList<Integer> list=new ArrayList<Integer>();
for (int j = 0; j < a[0].length; j++) {
l=0;
for (int i = 0; i < a.length; i++) {
if(a[i][j]=='o') {
l++;
}else {
if(l>=2) {
list.add(l);
}
l=0;
}
}
if(l>=2) {
list.add(l);
}
}
// 对于列表排序,常见的用法就是Colections.sort,默认从小到大,要想从大到小,需要使用Collections.reverseOrder()
Collections.sort(list,Collections.reverseOrder());
for(Integer x:list) {
if(k==0)break;
if(k>=x) {
count+=x-1;
k-=x;
}else if(k<x) {
count+=k-1;
k=0;
}
}
System.out.println(count);
}
}
这题属于贪心+排序
对于这样的矩阵,要想求出最高分数,那么性价比最高的走法就是不停往下走,并且连续的长度越长越好。
那么根据这个,我们就可以去求每一列中能获得分数的连续块长度为多少,然后把它装入列表中
最后我们要对列表进行从大到小排序,这里使用的方法是Collections.sort(list,Collections.reverseOrder());
排完序后我们就要开始遍历列表,长度为l,最多能够获得的分数为l-1,如果k<l,那么就是k-1



京公网安备 11010502036488号