https://www.acmicpc.net/problem/2615
오목은 바둑판에 검은 바둑알과 흰 바둑알을 교대로 놓아서 겨루는 게임이다. 바둑판에는 19개의 가로줄과 19개의 세로줄이 그려져 있는데 가로줄은 위에서부터 아래로 1번, 2번, ... ,19번의 번호가 붙고 세로줄은 왼쪽에서부터 오른쪽으로 1번, 2번, ... 19번의 번호가 붙는다.
위의 그림에서와 같이 같은 색의 바둑알이 연속적으로 다섯 알을 놓이면 그 색이 이기게 된다. 여기서 연속적이란 가로, 세로 또는 대각선 방향 모두를 뜻한다. 즉, 위의 그림은 검은색이 이긴 경우이다. 하지만 여섯 알 이상이 연속적으로 놓인 경우에는 이긴 것이 아니다.
입력으로 바둑판의 어떤 상태가 주어졌을 때, 검은색이 이겼는지, 흰색이 이겼는지 또는 아직 승부가 결정되지 않았는지를 판단하는 프로그램을 작성하시오. 단, 검은색과 흰색이 동시에 이기거나 검은색 또는 흰색이 두 군데 이상에서 동시에 이기는 경우는 입력으로 들어오지 않는다.
19줄에 각 줄마다 19개의 숫자로 표현되는데, 검은 바둑알은 1, 흰 바둑알은 2, 알이 놓이지 않는 자리는 0으로 표시되며, 숫자는 한 칸씩 띄어서 표시된다.
첫줄에 검은색이 이겼을 경우에는 1을, 흰색이 이겼을 경우에는 2를, 아직 승부가 결정되지 않았을 경우에는 0을 출력한다.
검은색 또는 흰색이 이겼을 경우에는 둘째 줄에 연속된 다섯 개의 바둑알 중에서 가장 왼쪽에 있는 바둑알(연속된 다섯 개의 바둑알이 세로로 놓인 경우, 그 중 가장 위에 있는 것)의 가로줄 번호와, 세로줄 번호를 순서대로 출력한다.
계속 틀려서 게시판에 있는 모든 반례 적용하면서 수정했다. DFS로 완전 탐색하는 로직이다. 6목이 되는 경우 실패 처리하는 것이 어려웠다. 5목 만드는 과정의 모든 좌표를 리스트에 저장해서 첫 좌표의 같은 방향의 이전 방향 좌표도 같은 색깔인지 체크하는 로직을 추가했다. 또한 5개 좌표 중에 가장 왼쪽 위의 좌표를 출력하는 로직을 추가해야했다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
private static final int[][] dirs = {{0, 1}, {1, 1}, {1, 0}, {1, -1}};
private static int winner = 0;
private static int[] winnerPosition = null;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[][] matrix = new int[19][19];
for (int i = 0; i < 19; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < 19; j++) {
matrix[i][j] = Integer.parseInt(st.nextToken());
}
}
br.close();
countAnswer(matrix);
}
private static void countAnswer(int[][] matrix) {
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
int number = matrix[i][j];
if (number != 0) {
for (int[] d : dirs) {
List<int[]> list = new ArrayList<>();
list.add(new int[] {i, j});
dfs(matrix, new int[] {i, j}, d, 1, list);
if (winner != 0) {
System.out.println(winner);
System.out.println((winnerPosition[0] + 1) + " " + (winnerPosition[1] + 1));
return;
}
}
}
}
}
System.out.println(winner);
if (winner != 0) {
System.out.println(19 + " " + 19);
}
}
private static void dfs(int[][] matrix, int[] start, int[] dir, int count, List<int[]> list) {
int dx = start[0];
int dy = start[1];
int nextDx = dx + dir[0];
int nextDy = dy + dir[1];
if (count == 5) {
if (nextDx >= 0 && nextDy >= 0 && nextDx < 19 && nextDy < 19 && matrix[dx][dy] == matrix[nextDx][nextDy]) {
return;
}
int[] initPosition = list.get(0);
int lastDx = initPosition[0] - dir[0];
int lastDy = initPosition[1] - dir[1];
if (lastDx >= 0 && lastDy >= 0 && lastDx < 19 && lastDy < 19 && matrix[dx][dy] == matrix[lastDx][lastDy]) {
return;
}
winner = matrix[dx][dy];
int colMin = 19;
int rowMin = 19;
for (int[] p : list) {
if (p[1] < colMin) {
colMin = p[1];
rowMin = p[0];
continue;
}
if (p[1] == colMin) {
rowMin = Math.min(rowMin, p[0]);
}
}
winnerPosition = new int[] {rowMin, colMin};
return;
}
if (nextDx >= 0 && nextDy >= 0 && nextDx < 19 && nextDy < 19 && matrix[dx][dy] == matrix[nextDx][nextDy]) {
list.add(new int[] {nextDx, nextDy});
dfs(matrix, new int[] {nextDx, nextDy}, dir, ++count, list);
}
}
}