fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Main {
  6. static int L, N;
  7. static int[] alpa_map;
  8. static int[][] map;
  9. static int[] ax = {-1, 1, 0, 0};
  10. static int[] ay = {0, 0, -1, 1};
  11. static int answer = 0;
  12. static int max = 0;
  13. public static void main(String[] args) throws IOException {
  14.  
  15. String[] n_arr = br.readLine().split(" ");
  16.  
  17. L = Integer.parseInt(n_arr[0]);
  18. N = Integer.parseInt(n_arr[1]);
  19.  
  20. map = new int[L][N];
  21.  
  22. alpa_map = new int[26];
  23.  
  24. for(int i = 0; i<L; i++) {
  25. String input = br.readLine();
  26. for(int j = 0; j<N; j++) {
  27. map[i][j] = (int)input.charAt(j) - 'A';
  28. }
  29. }
  30.  
  31. //alpa_map[map[0][0]] = 1;
  32.  
  33. answer++;
  34.  
  35. dfs(0, 0);
  36.  
  37. System.out.println(max);
  38. }
  39.  
  40. static void dfs(int i, int j) {
  41. alpa_map[map[i][j]] = 1;
  42. for(int a = 0; a<4; a++) {
  43. int nx = i + ax[a];
  44. int ny = j + ay[a];
  45.  
  46. if(nx >= 0 && ny >= 0 && nx < L && ny < N) {
  47. if(alpa_map[map[nx][ny]] == 0) {
  48. alpa_map[map[nx][ny]] = 1;
  49. answer++;
  50. if(max < answer) {
  51. max = answer;
  52. }
  53. dfs(nx, ny);
  54. }
  55. }
  56. }
  57.  
  58. answer--;
  59. alpa_map[map[i][j]] = 0;
  60. }
  61. }
  62.  
  63.  
Success #stdin #stdout 0.05s 2184192KB
stdin
2 2
AA
AA
stdout
0