새소식

알고리즘/부루트포스

[swea] 11315. 오목 판정(Java 풀이)

  • -

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AXaSUPYqPYMDFASQ 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

import java.util.Scanner;

public class Solution {
	static int[] dx = { -1, 0, 1, 1 };
	static int[] dy = { 1, 1, 1, 0 };
	static int N;
	static String ans;
	static char[][] arr;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 테스트케이스 수
		int T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			N = sc.nextInt();
			ans = "NO";
			sc.nextLine(); // 개행문자 제거
			arr = new char[N][];
			// 바둑판 입력
			for (int i = 0; i < N; i++) {
				String tmp = sc.nextLine();
				arr[i] = tmp.toCharArray();
			}
			// 오목 검증
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (arr[i][j] == 'o' && check(i, j)) {
						break;
					}
				}
			}
			System.out.printf("#%d %s\n", tc, ans);
		}
	}

	static boolean check(int x, int y) {
		for (int i = 0; i < 4; i++) {
			int cnt = 1;
			for (int j = 1; j < 5; j++) {
				int nx = x + dx[i] * j;
				int ny = y + dy[i] * j;
				if (nx < 0 || nx >= N || ny < 0 || ny >= N || arr[nx][ny] != 'o') {
					break;
				}
				cnt++;
			}
			if (cnt >= 5) {
				ans = "YES";
				return true;
			}
		}
		return false;
	}
}
Contents

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

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