-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alphabet.java
51 lines (43 loc) · 1.09 KB
/
Alphabet.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
package backtracking;
import java.util.HashSet;
import java.util.Scanner;
public class Alphabet {
static int R;
static int C;
static char chs[][];
static HashSet<Character> alpha = new HashSet<Character>();
static int max = 0;
private static void search(int n, int i, int j) {
alpha.add(chs[i][j]);
if (i - 1 >= 0)
if (!alpha.contains(chs[i - 1][j]))
search(n + 1, i - 1, j);
if (i + 1 < R)
if (!alpha.contains(chs[i + 1][j]))
search(n + 1, i + 1, j);
if (j - 1 >= 0)
if (!alpha.contains(chs[i][j - 1]))
search(n + 1, i, j - 1);
if (j + 1 < C)
if (!alpha.contains(chs[i][j + 1]))
search(n + 1, i, j + 1);
alpha.remove(chs[i][j]);
if (n > max)
max = n;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
R = scan.nextInt();
C = scan.nextInt();
chs = new char[R][C];
for (int i = 0; i < R; i++) {
String temp = scan.next();
for (int j = 0; j < C; j++) {
chs[i][j] = temp.charAt(j);
}
}
search(1, 0, 0);
System.out.println(max);
scan.close();
}
}