
https://www.acmicpc.net/problem/2615



import java.io.*;
import java.util.*;
public class Main {
private static int[][] board;
private static boolean[][] visited;
static int[] dx = {-1, 0, 1, 1};
static int[] dy = {1, 1, 1, 0};
static int count = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
board = new int[20][20];
visited = new boolean[20][20];
for (int i = 1; i <= 19; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= 19; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 1; i <= 19; i++) {
for (int j = 1; j <= 19; j++) {
int currentColor = board[i][j];
visited = new boolean[20][20];
if (currentColor != 0) {
for (int dir = 0; dir < 4; dir++) {
int x = i + dx[dir];
int y = j + dy[dir];
count = 1;
if (isValid(x, y) && board[x][y] == currentColor) {
visited[x][y] = true;
count++;
dfs(x, y, dir, currentColor);
}
if (count == 5 && checkFive(i, j, dir, currentColor)) {
bw.write(currentColor + "\n");
bw.write(i + " " +j);
bw.flush();
bw.close();
return;
}
}
}
}
}
bw.write('0');
bw.flush();
bw.close();
}
private static void dfs(int x, int y, int dir, int color) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (isValid(nx, ny) && !visited[nx][ny] && board[nx][ny] == color) {
count++;
visited[nx][ny] = true;
dfs(nx, ny, dir, color);
visited[nx][ny] = false;
}
}
private static boolean isValid(int x, int y) {
return x >= 1 && x <= 19 && y >= 1 && y <= 19;
}
private static boolean checkFive(int x, int y, int dir, int color) {
int beforeX = x - dx[dir];
int beforeY = y - dy[dir];
if (beforeX < 1 || beforeY < 1 || beforeX > 19 || beforeY > 19) {
return true;
}
return board[beforeX][beforeY] != color;
}
}