4중첩 for문 & 2차원 배열 델타 사용
import java.util.Scanner;
class Solution {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int[] dr = {1, 1, 1};
int[] dc = {1, 0, -1};
int T = sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt();
String ans = "NO";
char[][] arr = new char[N][N];
for(int i = 0; i < N; i++) {
String tempStr = sc.next();
arr[i] = tempStr.toCharArray();
if(tempStr.contains("ooooo")) {
ans = "YES";
}
}
if(ans.equals("NO")) {
all:for(int i = 0; i < N - 4; i++) {
for(int j = 0; j < N; j++) {
for(int d = 0; d < 3; d++) {
boolean bingo = true;
for(int k = 0; k < 5; k++) {
int nr = i + dr[d] * k;
int nc = j + dc[d] * k;
if(nr >= 0 && nr < N && nc >= 0 && nc < N) {
if(arr[nr][nc] == '.') {
bingo = false;
break;
}
} else {
bingo = false;
break;
}
}
if(bingo) {
ans = "YES";
break all;
}
}
}
}
}
System.out.printf("#%s %s%n", test_case, ans);
}
}
}
함수 생성 방법
import java.util.Scanner;
public class Solution {
static int[][] drc = { { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt();
char[][] arr = new char[N][N];
for (int i = 0; i < N; i++) {
arr[i] = sc.next().toCharArray();
}
boolean flag = false;
all: for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
if (arr[r][c] == 'o') {
flag = check(arr, r, c, N);
if (flag) {
break all;
}
}
}
}
if (flag) {
System.out.println("#" + test_case + " " + "YES");
} else {
System.out.println("#" + test_case + " " + "NO");
}
}
}
static boolean check(char[][] arr, int r, int c, int size) {
for (int d = 0; d < 4; d++) {
for (int k = 1; k <= 4; k++) {
int nr = r + drc[d][0] * k;
int nc = c + drc[d][1] * k;
if (nr >= 0 && nr < size
&& nc >= 0 && nc < size
&& arr[nr][nc] == 'o') {
if (k == 4) {
return true;
}
} else {
break;
}
}
}
return false;
}
}