-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboggle.java
More file actions
executable file
·106 lines (105 loc) · 2.75 KB
/
boggle.java
File metadata and controls
executable file
·106 lines (105 loc) · 2.75 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.*;
import java.io.*;
public class boggle {
static char[][] board;
static boolean [][] visited;
public static void main(String[] args)throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int w = Integer.parseInt(bf.readLine());
String [] dictionary = new String[w];
for(int i=0;i<w;i++){
dictionary[i] = bf.readLine();
}
bf.readLine();
int numboards = Integer.parseInt(bf.readLine());
for(int i=0;i<numboards;i++){
board = new char[4][4];
for(int j=0;j<4;j++){
board[j] = bf.readLine().toCharArray();
}
bf.readLine();
visited = new boolean[4][4];
ArrayList<String> fin = search(board,dictionary);
int score = 0;
int count = 0;
for(int d=0;d<fin.size();d++){
if(fin.get(d).length()<3){
count++;
}
if(fin.get(d).length()==3||fin.get(d).length()==4){
score = score+1;
count++;
}
if(fin.get(d).length()==5){
score = score+2;
count++;
}
if(fin.get(d).length()==6){
score = score+3;
count++;
}
if(fin.get(d).length()==7){
score = score+5;
count++;
}
if(fin.get(d).length()==8){
score = score+11;
count++;
}
}
String biggest = "";
for(int m=0;m<fin.size();m++){
if (fin.get(m).length() > biggest.length() || (fin.get(m).length() == biggest.length() && fin.get(m).compareTo(biggest) < 0)) {
biggest = fin.get(m);
}
}
System.out.println(score+" "+biggest+" "+count);
}
}
public static void toString(char[][]array){
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static ArrayList<String> search(char[][]board,String[]dictionary){
ArrayList<String> f = new ArrayList<String>();
Set<String> foundWords = new HashSet<String>();
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
for(int k=0;k<dictionary.length;k++){
if(board[i][j]==dictionary[k].charAt(0)){
if(find(i,j,dictionary[k],1)==true){
f.add(dictionary[k]);
}
}
}
}
}
Set<String> hs = new HashSet<>();
hs.addAll(f);
f.clear();
f.addAll(hs);
return f;
}
public static boolean find(int rows,int columns,String word,int position){
int[] x = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] y = {-1, 0, 1, -1, 1, -1, 0, 1};
if(position==word.length()){
return true;
}
boolean res = false;
visited[rows][columns] = true;
for(int i=0;i<8;++i){
int numx = rows+x[i];
int numy = columns+y[i];
if (0 <= numx && numx < 4 && 0 <= numy && numy < 4 && !visited[numx][numy] && board[numx][numy] == word.charAt(position)) {
res = res || find(numx, numy, word, position + 1);
}
}
visited[rows][columns] = false;
return res;
}
}