https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AXaSUPYqPYMDFASQ
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
class Solution {
static int[] dx = {1, -1, 0, 0, 1, 1};
static int[] dy = {0, 0, 1, -1, 1, -1};
static int n;
static char[][] board;
static boolean find;
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 1; t <= T ; t++) {
n = sc.nextInt();
board= new char[n][n];
for (int i = 0; i < n; i++) {
String line = sc.next();
for (int j = 0; j < n; j++) {
board[i][j] = line.charAt(j);
}
}
find = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] != 'o') continue;
if (find) break;
for (int k = 0; k < 6; k++) {
dfs(i, j, 0, dx[k], dy[k]);
if (find) break;
}
}
}
System.out.println("#" + t + " " + (find ? "YES" : "NO"));
}
}
static void dfs(int x, int y, int depth, int movex, int movey){
if(depth == 4 && board[x][y] == 'o'){
find = true;
return;
}
int nx = x + movex;
int ny = y + movey;
if(nx < 0 || nx >= n || ny < 0 || ny >= n) return;
if(board[nx][ny] != 'o') return;
dfs(nx, ny,depth+1, movex, movey);
}
}
'알고리즘 > 삼성 SW expert Academy' 카테고리의 다른 글
[SWEA] 10726. 이진수 표현_JAVA (0) | 2022.05.21 |
---|---|
[SWEA] 10804. 문자열의 거울상_JAVA (0) | 2022.05.21 |
[SWEA] 11856. 반반_JAVA (0) | 2022.05.18 |
[SWEA] 12741. 두 전구_JAVA (0) | 2022.05.17 |
[SWEA] 13229. 일요일_JAVA (0) | 2022.05.17 |