-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTofuCutSol213.java
78 lines (65 loc) · 1.55 KB
/
TofuCutSol213.java
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
72
73
74
75
76
77
78
// for more info
// http://softwareeng.tistory.com/category/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98
import java.util.Scanner;
public class TofuCutSol213 {
public static int cu(){
return 1<<n;
}
public static int rt(){
return 1<<(n-1);
}
public static int dn(){
return 1;
}
public static int M(){
return 1<<(n+1);
}
public static int[][] p = new int[][]{
{100,70, 40, 0},
{70, 50, 30, 0},
{40, 30, 20, 0},
{0, 0, 0, 0, 0}
};
public static int n;
public static int[][] tb = new int[12][12];
public static int[][][] m = new int[12][12][1<<13];
public static int max(int a, int b){
return a > b ? a : b;
}
public static int f(int x, int y, int s){
if (x==n)
return 0;
if (y==n)
return f(x+1, 0, s);
if (m[x][y][s]<=0){
if ((s&cu())<=0){
if (y+1<n && (s&rt())<=0)
m[x][y][s]=max(m[x][y][s],
f(x,y+2,(s<<2)%M())+p[tb[x][y]][tb[x][y+1]]);
if (x+1 < n && (s&dn())<=0)
m[x][y][s]=max(m[x][y][s],
f(x,y+1,((s|dn())<<1)%M())+p[tb[x][y]][tb[x+1][y]]);
m[x][y][s]=max(m[x][y][s], f(x,y+1,(s<<1)%M()));
}
else
m[x][y][s]=max(m[x][y][s], f(x,y+1,(s<<1)%M()));
}
return m[x][y][s];
}
public static void main(String[] args) {
int i, j;
char t;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (i=0; i<n; i++){
String temp = sc.next();
for (j=0; j<n; j++){
t = temp.charAt(j);
tb[i][j]=(t=='F'? 3: t-'A');
}
}
sc.close();
System.out.println(f(0,0,0));
return;
}
}