|
| 1 | +package week7.벌꿀채취; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class SWEA_2115 { |
| 11 | + static class Honey{ |
| 12 | + int row; |
| 13 | + int col; |
| 14 | + int amount; |
| 15 | + boolean visited; |
| 16 | + |
| 17 | + public Honey(int row, int col, int amount, boolean visited){ |
| 18 | + this.row = row; |
| 19 | + this.col = col; |
| 20 | + this.amount = amount; |
| 21 | + this.visited = visited; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + static int price; |
| 26 | + static int price_b; |
| 27 | + static int subset_sum; |
| 28 | + static int max_price; |
| 29 | + static int n, m, c; |
| 30 | + static Honey[][] map; |
| 31 | + static boolean[][] visited; |
| 32 | + static List<Honey> a_honey; |
| 33 | + static List<Honey> b_honey; |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 36 | + StringTokenizer st; |
| 37 | + int T = Integer.parseInt(br.readLine()); |
| 38 | + |
| 39 | + for(int tc=0; tc<T; tc++){ |
| 40 | + st = new StringTokenizer(br.readLine()); |
| 41 | + |
| 42 | + n = Integer.parseInt(st.nextToken()); |
| 43 | + m = Integer.parseInt(st.nextToken()); |
| 44 | + c = Integer.parseInt(st.nextToken()); |
| 45 | + |
| 46 | + map = new Honey[n][n]; |
| 47 | + for(int i=0; i<n; i++){ |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + for(int j=0; j<n; j++){ |
| 50 | + map[i][j] = new Honey(i, j, Integer.parseInt(st.nextToken()), false); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + visited = new boolean[n][n]; |
| 55 | + a_honey = new ArrayList<>(); |
| 56 | + b_honey = new ArrayList<>(); |
| 57 | + max_price = 0; |
| 58 | + |
| 59 | + combination(0, 0); |
| 60 | + |
| 61 | + System.out.println(max_price); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static int getSum(List<Honey> honeys, boolean[] selected){ |
| 66 | + int sum = 0; |
| 67 | + for(int i=0; i<honeys.size(); i++){ |
| 68 | + if(selected[i]){ |
| 69 | + sum+=honeys.get(i).amount; |
| 70 | + } |
| 71 | + } |
| 72 | + return sum; |
| 73 | + } |
| 74 | + |
| 75 | + static void subset(List<Honey> honey, int depth, boolean[] selected){ |
| 76 | + if(getSum(honey, selected) > c)return; |
| 77 | + |
| 78 | + if(depth == m){ |
| 79 | + price = Math.max(subset_sum, getSum(honey, selected)); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + selected[depth] = true; |
| 84 | + subset(honey, depth+1, selected); |
| 85 | + |
| 86 | + selected[depth] = false; |
| 87 | + subset(honey, depth+1, selected); |
| 88 | + } |
| 89 | + |
| 90 | + static void updateMaxPrice(List<List<Honey>> selected){ |
| 91 | + //System.out.println(selected.size()); |
| 92 | + subset_sum = 0; |
| 93 | + for(List<Honey> honeys : selected){ |
| 94 | + price = 0; |
| 95 | + subset(honeys, 0, new boolean[m]); |
| 96 | + subset_sum += price; |
| 97 | + } |
| 98 | + max_price = Math.max(subset_sum, max_price); |
| 99 | + } |
| 100 | + |
| 101 | + static void combination(int startRow, int depth){ |
| 102 | + if(depth==2){ |
| 103 | +// System.out.println("=================="); |
| 104 | +// System.out.println(); |
| 105 | +// System.out.println("[a의 꿀통]"); |
| 106 | +// for(Honey a : a_honey){ |
| 107 | +// System.out.println(a.row+" "+a.col); |
| 108 | +// } |
| 109 | +// System.out.println(); |
| 110 | +// |
| 111 | +// System.out.println("[b의 꿀통]"); |
| 112 | +// for(Honey b : b_honey){ |
| 113 | +// System.out.println(b.row+" "+b.col); |
| 114 | +// } |
| 115 | +// System.out.println(); |
| 116 | + |
| 117 | + //updateMaxPrice(); |
| 118 | + |
| 119 | + List<List<Honey>> selected = new ArrayList<>(); |
| 120 | + selected.add(a_honey); |
| 121 | + selected.add(b_honey); |
| 122 | + |
| 123 | + updateMaxPrice(selected); |
| 124 | + |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + for(int i=startRow; i<n; i++){ |
| 129 | + for(int j=0; j<n; j++){ |
| 130 | + if(map[i][j].visited)continue; |
| 131 | + |
| 132 | + int nextCol = j; |
| 133 | + boolean flag = true; |
| 134 | + int size = m; |
| 135 | + |
| 136 | + //m개의 연속된 꿀통 고르기 |
| 137 | + while(size-- > 1){ |
| 138 | + nextCol++; |
| 139 | + if((nextCol>=n && size>0) || map[i][nextCol].visited){ |
| 140 | + flag = false; |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if(flag){ |
| 146 | + if(a_honey.size()==0){ |
| 147 | + addHoney(a_honey, i, j, nextCol); |
| 148 | + combination(i, depth+1); |
| 149 | + rollBack(a_honey); |
| 150 | + } |
| 151 | + else{ |
| 152 | + addHoney(b_honey, i, j, nextCol); |
| 153 | + combination(i, depth+1); |
| 154 | + rollBack(b_honey); |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + static void addHoney(List<Honey> honeys, int row, int startCol, int endCol){ |
| 163 | + for(int i=startCol; i<=endCol; i++){ |
| 164 | + map[row][i].visited = true; |
| 165 | + honeys.add(map[row][i]); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + static void rollBack(List<Honey> honeys){ |
| 170 | + for(Honey h : honeys){ |
| 171 | + int row = h.row; |
| 172 | + int col = h.col; |
| 173 | + map[row][col].visited = false; |
| 174 | + } |
| 175 | + honeys.clear(); |
| 176 | + } |
| 177 | +} |
0 commit comments