SSAFY/SWEA

[SWEA] 11315 : 오목판정

믕비 2023. 5. 3. 19:24

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AXaSUPYqPYMDFASQ&categoryId=AXaSUPYqPYMDFASQ&categoryType=CODE&problemTitle=&orderBy=SUBMIT_COUNT&selectCodeLang=JAVA&select-1=3&pageSize=10&pageIndex=3 

 

SW Expert Academy

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

swexpertacademy.com

내 코드

메모리 : 17692 KB

시간 : 108 ms

코드길이 : 1508 B

 

[내 코드]

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

public class Solution {
	static int N;
	static char[][] graph;
	//상하좌우대각선4방향
	static int[] dir_x = {-1, -1, -1, 0, 0, 1, 1, 1};
	static int[] dir_y = {-1, 0, 1, -1, 1, -1, 0, 1};
	static boolean result;

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		for(int t = 1; t <= T; t++) {
			sb.append('#').append(t).append(' ');
			N = Integer.parseInt(br.readLine());
			graph = new char[N][N];
			result = false;
			
			for(int i = 0; i < N; i++) {
				String row = br.readLine();
				for(int j = 0; j < N; j++) {
					graph[i][j] = row.charAt(j);
				}
			}
			
			for(int i = 0; i < N; i++) {
				for(int j = 0; j < N; j++) {
					if(graph[i][j] == 'o') {
						for(int dir = 0; dir < 8; dir++) {
							search(i, j, dir, 1);
						}
					}
				}
			}
			sb.append(result ? "YES" : "NO").append('\n');
		}
		System.out.print(sb);
	}
	
	public static void search(int row, int column, int dir, int count) {
		if(count == 5) {
			result = true;
			return;
		}
		
		int nextRow = row + dir_x[dir];
		int nextColumn = column + dir_y[dir];
		
		if(nextRow < 0 || nextRow == N || nextColumn < 0 || nextColumn == N)
			return;
		
		if(graph[nextRow][nextColumn] == 'o')
			search(nextRow, nextColumn, dir, count + 1);
		
		return;
	}

}

'SSAFY > SWEA' 카테고리의 다른 글

[SWEA] 1952 : 수영장  (0) 2023.05.04
[SWEA] 3750 : Digit sum  (0) 2023.05.03
[SWEA] 2817 : 부분 수열의 합  (0) 2023.05.03
[SWEA] 4408 : 자기 방으로 돌아가기  (0) 2023.05.03
[SWEA] 6485 : 삼성시의 버스 노선  (0) 2023.05.02