-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDollDrawing.java
More file actions
47 lines (44 loc) · 1.33 KB
/
DollDrawing.java
File metadata and controls
47 lines (44 loc) · 1.33 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
import java.util.Scanner;
import java.util.Stack;
public class DollDrawing {
public int solution(int n, int[][] board, int m, int[] moves) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < m; i++) {
int k = moves[i] - 1;
for (int j = 0; j < n; j++) {
int x = board[j][k];
if (x == 0) {
continue;
}
board[j][k] = 0;
if (!stack.isEmpty() && stack.peek() == x) {
stack.pop();
answer+=2;
}
else {
stack.push(x);
}
break;
}
}
return answer;
}
public static void main(String[] args) {
DollDrawing t = new DollDrawing();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] board = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = scanner.nextInt();
}
}
int m = scanner.nextInt();
int[] moves = new int[m];
for (int i = 0; i < m; i++) {
moves[i] = scanner.nextInt();
}
System.out.println(t.solution(n, board, m, moves));
}
}