알고리즘/JA

1733 : 오목

  • -
반응형

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=1006&sca=2040

 

JUNGOL | 오목 > 문제은행

제한시간: 1000 ms    메모리제한: 32 MB 해결횟수: 1262 회    시도횟수: 7135 회    오목은 바둑판에 검은 바둑알과 흰 바둑알을 교대로 놓아서 겨루는 게임이다.  바둑판에는 19개의 가로줄과 19개의 세로줄이 그려져 있는데  가로줄은 위에서부터 아래로 1번, 2번, ... ,19번의 번호가 붙고  세로줄은 왼쪽에서부터 오른쪽으로 1번, 2번, ... 19번의 번호가 붙는다. 위의 그림에서와 같이 같은 색의 바둑알이 연속적으로 다섯

www.jungol.co.kr

속도는 좀 빨라졌는데 많이 너저분해진 코드

문제 해석시 약간의 오해가 있을 수 있는 부분

 - 가로줄 번호와 세로줄 번호를 순서대로 출력하는데 잘 생각해보면 가로줄이 행번호, 세로줄이 열번호이다.

 

 

public class Main {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StringBuilder sb = new StringBuilder();
	static StringTokenizer tokens;
	static int[][] map = new int[20][20];	// 바둑판이 1부터 시작한다.

	public static void main(String[] args) throws NumberFormatException, IOException {
		for (int i = 1; i < 20; i++) {
			tokens = new StringTokenizer(br.readLine());
			for (int j = 1; j < 20; j++) {
				map[i][j] = Integer.parseInt(tokens.nextToken());
			}
		}
		int[] answer = null;
		outer: for (int row = 1; row < 20; row++) {
			for (int col = 1; col < 20; col++) {
				if (map[row][col] != 0) {
					answer = checkHorizental(row, col, map[row][col]);
					if (answer != null) {
						break outer;
					} else {
						answer = checkVertical(row, col, map[row][col]);
						if (answer != null) {
							break outer;
						} else {
							answer = cross135(row, col, map[row][col]);
							if (answer != null) {
								break outer;
							} else {
								answer = cross45(row, col, map[row][col]);
								if (answer != null) {
									break outer;
								}
							}
						}
					}
				}
			}
		}
		if (answer != null) {
			sb.append(map[answer[0]][answer[1]]).append("\n").append(answer[0]).append(" ").append(answer[1]);
		} else {
			sb.append("0");
		}
		System.out.println(sb);
	}

	// dir==1: 상하
	private static int[] checkVertical(int row, int col, int stone) {
		int cnt = 1;
		int[] answer = { row, col };
		for (int i = row - 1; i >= 0; i--) {
			if (stone == map[i][col]) {
				cnt++;
				answer[0] = i;
				answer[1] = col;
			} else {
				break;
			}
		}
		for (int i = row + 1; i < 20; i++) {
			if (stone == map[i][col]) {
				cnt++;
			} else {
				break;
			}
		}
		return cnt == 5 ? answer : null;
	}

	private static int[] checkHorizental(int row, int col, int stone) {
		int cnt = 1;
		int[] answer = { row, col };
		for (int i = col - 1; i >= 0; i--) {
			if (stone == map[row][i]) {
				cnt++;
				answer[0] = row;
				answer[1] = i;
			} else {
				break;
			}
		}
		for (int i = col + 1; i < 20; i++) {
			if (stone == map[row][i]) {
				cnt++;
			} else {
				break;
			}
		}
		return cnt == 5 ? answer : null;
	}

	// ↘
	private static int[] cross135(int row, int col, int stone) {
		int cnt = 1;
		int[] answer = { row, col };
		for (int i = 1; i < 20; i++) {
			int nRow = row - i;
			int nCol = col - i;
			if (isIn(nRow, nCol) && map[nRow][nCol] == stone) {
				cnt++;
				answer[0] = nRow;
				answer[1] = nCol;
			} else {
				break;
			}
		}
		for (int i = 1; i < 20; i++) {
			int nRow = row + i;
			int nCol = col + i;
			if (isIn(nRow, nCol) && map[nRow][nCol] == stone) {
				cnt++;
			} else {
				break;
			}
		}
		return cnt == 5 ? answer : null;
	}

	// ↗
	private static int[] cross45(int row, int col, int stone) {
		int cnt = 1;
		int[] answer = { row, col };
		for (int i = 1; i < 20; i++) {
			int nRow = row - i;
			int nCol = col + i;
			if (isIn(nRow, nCol) && map[nRow][nCol] == stone) {
				cnt++;
			} else {
				break;
			}
		}
		for (int i = 1; i < 20; i++) {
			int nRow = row + i;
			int nCol = col - i;
			if (isIn(nRow, nCol) && map[nRow][nCol] == stone) {
				cnt++;
				answer[0] = nRow;
				answer[1] = nCol;
			} else {
				break;
			}
		}
		return cnt == 5 ? answer : null;
	}

	private static boolean isIn(int row, int col) {
		return 0 <= row && 0 <= col && row < 20 && col < 20;
	}
}
반응형

'알고리즘 > JA' 카테고리의 다른 글

1127 : 맛있는 음식(PERKET)  (0) 2019.08.15
1809 : 탑  (0) 2019.08.08
1681 : 해밀턴 순환회로  (0) 2019.07.28
[솔루션]정올 1810 백설공주  (0) 2019.07.23
[솔루션]정올 1169 주사위 던지기 1  (0) 2018.10.22
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.