-
Notifications
You must be signed in to change notification settings - Fork 0
/
D4P2.java
65 lines (60 loc) · 1.69 KB
/
D4P2.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
import java.util.*;
import java.io.*;
public class Main {
static boolean checkWin(String[][] board) {
for(int i=0; i<board.length; i++) {
int iCount = 0;
int jCount = 0;
for(int j=0; j<board[i].length; j++) {
if(board[i][j] == null) ++iCount;
if(board[j][i] == null) ++jCount;
}
if(iCount >= 5 || jCount >= 5) return true;
}
return false;
}
//I pressed enter for the first time in my life
static int getScore(String[][] board, int call) {
int sum = 0;
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[i].length; j++) {
if(board[i][j] != null) sum += Integer.parseInt(board[i][j]);
}
}
return sum * call;
}
static void clearCalls(String[][] board, String call) {
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[i].length; j++) {
if(board[i][j] != null && board[i][j].equals(call)) board[i][j] = null;
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String callNums = bf.readLine();
String[][][] boards = new String[100][5][5];
for(int i=0; i<100; i++) {
bf.readLine();
for(int j=0; j<5; j++) boards[i][j] = bf.readLine().trim().split("\\s+");
}
StringTokenizer calls = new StringTokenizer(callNums, ",");
int boardsWon = 0;
outer:
while(calls.hasMoreTokens()) {
String call = calls.nextToken();
for(int i=0; i<boards.length; i++) {
if(boards[i] == null) continue;
clearCalls(boards[i], call);
if(checkWin(boards[i])) {
boardsWon++;
if(boardsWon == 100) {
System.out.println(getScore(boards[i], Integer.parseInt(call)));
break outer;
}
boards[i] = null;
}
}
}
}
}