본문 바로가기

OJ

[BOJ] 2615 오목 (JAVA)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int[][] arr = new int[19][19];
		int[] dr = {  0, 0, 0, 0, 0, 0, -1, 1, 2, 3, 4, 5, -1, 1, 2, 3, 4, 5,  1, -1, -2, -3, -4, -5 };	// 길이 24
		int[] dc = { -1, 1, 2, 3, 4, 5,  0, 0, 0, 0, 0, 0, -1, 1, 2, 3, 4, 5, -1,  1,  2,  3,  4,  5 };	// 길이 24
		boolean flag = false;
		int[] idx = new int[2];

		/* 배열에 원소 삽입 */
		for (int i = 0; i < 19; i++) {
			
			String str = br.readLine();
			StringTokenizer st = new StringTokenizer(str);
			
			for (int j = 0; j < 19; j++) {
				
				arr[i][j] = Integer.parseInt(st.nextToken());
				
			}
			
		} /* 원소 삽입 끝 */
		
		/* 5번 연속된(이긴) 돌이 있는지 확인 */
		for (int i = 0; i < 19 && flag == false; i++) {
			
			for (int j = 0; j < 19 && flag == false; j++) {
				
				if(arr[i][j] != 0) {
					
					int now = arr[i][j];
					int cnt = 1;
					
					for (int k = 0; k < 24; k++) {
						
						/* 다음 방문할 인덱스를 저장할 nextI, nextJ */
						int nextI = i + dr[k];
						int nextJ = j + dc[k];

						/* 다른 방향으로 뻗기 시작할 때마다 cnt 초기화 */
						if(k % 6 == 0) {
							
							cnt = 1;
							
						}
						
						/* 배열 범위를 벗어나지 않으면서, 다음 방문할 곳의 돌과 색이 같으면 */
						if(0 <= nextI && nextI < 19 && 0 <= nextJ && nextJ < 19 && arr[nextI][nextJ] == now) {
							
							/* 육목을 처리하기 위해 둔 dr, dc 배열의 0, 6, 12, 18번 원소
							   그 원소들은 색이 같아봐야 필요 없어서 바로 다른 방향으로 뻗게 k가 조정됨 */
							if(k % 6 == 0) {
								
								k += 5;
								continue;
								
							}
							
							/* 마찬가지로 육목을 처리하기 위한 5, 11, 17, 23번 원소도 색이 같아봐야 필요가 없다 */
							if(k % 6 == 5) {
								
								continue;
								
							} else {
								
								cnt++;
								
							}
							
						}
						
						/* 연속된 5개의 돌을 찾았을 때 */
						if(k % 6 == 5 && cnt == 5) {

							idx[0] = i;
							idx[1] = j;
							flag = true;
							break;
							
						}
						
					}
					
				}
				
			}
			
		} /* 5번 연속된(이긴) 돌이 있는지 확인 끝 */
		
		if(flag) {

			System.out.println(arr[idx[0]][idx[1]]);
			System.out.printf("%d %d", idx[0] + 1, idx[1] + 1);
			
		} else {
			
			System.out.print(0);
			
		}
		
	}
	
}

 

'OJ' 카테고리의 다른 글

[SWEA] 1218 괄호 짝짓기 (JAVA)  (0) 2022.08.03
[SWEA] 1210 Ladder1 (JAVA)  (0) 2022.08.02
[BOJ] 1543 문서 검색 (JAVA)  (0) 2022.07.24
[BOJ] 15552 빠른 A+B (JAVA)  (0) 2022.07.17
[BOJ] 10953 A+B - 6 (JAVA)  (0) 2022.07.14