- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14391.java
More file actions
Latest commit
71 lines (57 loc) · 2.13 KB
/
Copy path14391.java
File metadata and controls
71 lines (57 loc) · 2.13 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
intm = sc.nextInt();
int[][] map = newint[n][m];
sc.nextLine();
for(inti=0; i<n; i++) {
Stringstr = sc.nextLine().trim();
String[] arr = str.split("");
for(intj=0; j<m; j++) {
map[i][j] = Integer.valueOf(arr[j]);
}
}
intmaxSum = 0;
// MAIN idea == 모든 칸은 가로로 더해지거나 세로로 더해지거나 2가지 경우를 가짐
// 비트마스크 활용. n x m 의 행렬을 1차원 배열로 나타내고
// 0은 가로향 합, 1은 세로방향 합으로 진행
for(inttc=0; tc < (1 << n*m); tc++) {
StringtcStr = String.format("%"+ Integer.toString(m*n) + "s", Integer.toBinaryString(tc)).replace(" ", "0");
// 가로방향 조각들의 합 구하기
introwTotalSum = 0;
for(introwIdx=0; rowIdx<n; rowIdx++) {
introwSum = 0;
for(intcolIdx=0; colIdx<m; colIdx++) {
if(tcStr.charAt(m*rowIdx + colIdx) == '0')
rowSum = rowSum * 10 + map[rowIdx][colIdx];
else {
rowTotalSum += rowSum;
rowSum = 0;
}
}
rowTotalSum += rowSum;
}
// 세로방향 조각들의 합 구하기
intcolTotalSum = 0;
for(intcolIdx=0; colIdx<m; colIdx++) {
intcolSum = 0;
for(introwIdx=0; rowIdx<n; rowIdx++) {
if(tcStr.charAt(m*rowIdx + colIdx) == '1')
colSum = colSum * 10 + map[rowIdx][colIdx];
else {
colTotalSum += colSum;
colSum = 0;
}
}
colTotalSum += colSum;
}
// 전체 합
intsum = rowTotalSum + colTotalSum;
if(maxSum < sum)
maxSum = sum;
}
System.out.println(maxSum);
}
}