fork download
  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3.  
  4. public class Main {
  5. static int[][] board = new int[19][19];
  6. static int[] dy = {1, -1, 0, 1};
  7. static int[] dx = {0, 1, 1, 1};
  8.  
  9. public static void main(String[] args) throws IOException {
  10. boolean flag = false;
  11.  
  12. for(int row=0; row<19; row++) {
  13. int col = 0;
  14. StringTokenizer st = new StringTokenizer(br.readLine(), " ");
  15.  
  16. while(st.hasMoreTokens()) {
  17. board[row][col++] = Integer.parseInt(st.nextToken());
  18. }
  19. }
  20.  
  21. for(int col=0; col<19; col++) {
  22. for(int row=0; row<19; row++) {
  23. if(board[row][col] == 0)
  24. continue;
  25.  
  26. for(int dir=0; dir<4; dir++){
  27. if(!isValidateNum(row + dy[dir]) || !isValidateNum(col + dx[dir])) {
  28. continue;
  29. }
  30.  
  31. if(search(row, col, dir, 1)) {
  32. bw.write(board[row][col] + "\n");
  33. bw.write((row+1) + " " + (col+1));
  34. flag = true;
  35. break;
  36. }
  37. }
  38. }
  39.  
  40. if(flag) {
  41. break;
  42. }
  43. }
  44. if(!flag) {
  45. bw.write("0");
  46. }
  47. bw.flush();
  48. }
  49.  
  50. public static boolean search(int row, int col, int dir, int cnt) {
  51. if(cnt == 5) {
  52. if ((!isValidateNum(row + dy[dir]) || !isValidateNum(col + dx[dir])) || board[row + dy[dir]][col + dx[dir]] != board[row][col]) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58.  
  59. if(board[row + dy[dir]][col + dx[dir]] == board[row][col]) {
  60. if(cnt < 5 && (!isValidateNum(row + dy[dir]) || !isValidateNum(col + dx[dir]))) {
  61. return false;
  62. }
  63.  
  64. if(search(row + dy[dir], col + dx[dir], dir, cnt+1)) {
  65. return true;
  66. }
  67. }
  68.  
  69. return false;
  70. }
  71.  
  72. public static boolean isValidateNum(int num) {
  73. if(num < 0 || num >= 19) {
  74. return false;
  75. }
  76.  
  77. return true;
  78. }
  79. }
Runtime error #stdin #stdout #stderr 0.08s 47152KB
stdin
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 19 out of bounds for length 19
	at Main.search(Main.java:61)
	at Main.search(Main.java:66)
	at Main.search(Main.java:66)
	at Main.search(Main.java:66)
	at Main.main(Main.java:33)