算法 BISHI45 小红的矩阵染色 流程图 纵向遍历矩阵统计 贪心排序与 Score 计算 求解代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); String[] str = br.readLine().split("\\s+"); int n = Integer.parseInt(str[0]); int m = Integer.parseInt(str[1]); int k = Integer.parseInt(str[2]); char[][] matrix = new char[n][m]; for(int i=0;i<n;i++){ matrix[i]=br.readLine().toCharArray(); } List<Integer> block = new ArrayList<>(); for(int j = 0;j<m;j++){ int current = 0; for(int i=0;i<n;i++){ if(matrix[i][j]=='o'){ current++; }else{ if(current>=2){ block.add(current); } current = 0; } } if(current>=2){ block.add(current); } } Collections.sort(block,Collections.reverseOrder()); int score = 0; for(int len:block){ if(k==0){ break; } int cell = Math.min(k, len); if(cell>=2){ score += cell-1; } k-=cell; } out.println(score); out.flush(); out.close(); br.close(); }