[Codetree] 오목
30 Sep 2026
Reading time ~2 minutes
해당 문제는 코드트리 오목에서 풀어보실 수 있습니다.
풀이1 구현
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[19][19];
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
arr[i][j] = sc.nextInt();
}
}
// Please write your code here.
int dy[] = {1,0,-1,0,1,1};
int dx[] = {0,1,0,-1,1,-1};
for(int i = 0; i < 19; i++) {
for(int j = 0; j < 19; j++) {
if(arr[i][j]==0) continue;
for(int dir = 0; dir < 6; dir++){
int cnt = 0;
for(int k = 0; k < 5; k++) {
int y = i + dy[dir]*k;
int x = j + dx[dir]*k;
if(isOutOfBound(y,x)) break;
if(arr[y][x] != arr[i][j]) break;
cnt++;
if(cnt == 5) {
System.out.println(arr[i][j]);
System.out.println((y-dy[dir]*2 + 1) +" "+(x-dx[dir]*2 + 1));
return;
}
}
}
}
}
System.out.println(0);
}
public static boolean isOutOfBound(int y, int x) {
if(y < 0 || y >= 19 || x < 0 || x >= 19) return true;
return false;
}
}
풀이2 구현
import java.util.Scanner;
public class Main {
public static final int DIR_NUM = 8;
public static int[][] arr = new int[19][19];
public static int[] dx = new int[]{1, 1, 1, -1, -1, -1, 0, 0};
public static int[] dy = new int[]{-1, 0, 1, -1, 0, 1, -1, 1};
public static boolean inRange(int x, int y) {
return 0 <= x && x < 19 && 0 <= y && y < 19;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 입력을 받습니다.
for(int i = 0; i < 19; i++)
for(int j = 0; j < 19; j++)
arr[i][j] = sc.nextInt();
// 모든 좌표에서 다 확인해봅니다.
for(int i = 0; i < 19; i++) {
// 격자를 벗어나지 않을 범위로만 잡습니다.
for(int j = 0; j < 19; j++) {
if(arr[i][j] == 0) continue;
for(int k = 0; k < DIR_NUM; k++) {
int curt = 1;
int curx = i;
int cury = j;
while(true) {
int nx = curx + dx[k];
int ny = cury + dy[k];
if(inRange(nx, ny) == false)
break;
if(arr[nx][ny] != arr[i][j])
break;
curt++;
curx = nx;
cury = ny;
}
if(curt == 5) {
System.out.println(arr[i][j]);
System.out.println((i + 2 * dx[k] + 1) + " " + (j + 2 * dy[k] + 1));
System.exit(0);
}
}
}
}
System.out.println(0);
}
}