-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1799.java
More file actions
77 lines (68 loc) · 2.09 KB
/
Copy path1799.java
File metadata and controls
77 lines (68 loc) · 2.09 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
72
73
74
75
76
77
// https://www.acmicpc.net/problem/1799
// 비숍
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n;
static List<Node> black, white;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(reader.readLine());
black = new ArrayList<>();
white = new ArrayList<>();
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(reader.readLine());
for(int j=0; j<n; j++){
if(st.nextToken().charAt(0) == '1'){
if(isBlack(i, j)) black.add(new Node(i, j));
else white.add(new Node(i, j));
}
}
}
long startTime = System.currentTimeMillis();
System.out.println(solution(black) + solution(white));
//System.out.println(System.currentTimeMillis()-startTime+"ms");
}
static int max, end;
static List<Node> positions, cases;
public static int solution(List<Node> list){
max = 0;
end = list.size();
positions = new ArrayList<>();
cases = list;
dfs(0);
return max;
}
public static void dfs(int idx){
if(idx == end) return;
if(check(positions, cases.get(idx))){
positions.add(cases.get(idx));
max = Math.max(max, positions.size());
dfs(idx+1);
positions.remove(cases.get(idx));
}
dfs(idx+1);
}
public static boolean check(List<Node> list, Node cur){
for(Node node : list)
if(cur.isConfront(node))
return false;
return true;
}
public static boolean isBlack(int x, int y){ return x%2 == y%2; }
}
class Node{
int x, y;
public Node(int x, int y){
this.x = x;
this.y = y;
}
public boolean isConfront(Node o){
return Math.abs(this.x - o.x) == Math.abs(this.y - o.y);
}
@Override
public String toString(){
return "("+x+", "+y+")";
}
}