19 x 19 에서 흑/백/결정x 3가지 조건 확인
5번 동일한 방향으로 움직여야 함
import java.io.*;
import java.util.*;
public class Main {
static int[][] way = {{-1,0},{1,0},{0,1},{1,1},{-1,1}};
static int[][] box;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
box = new int[19][19];
for (int i = 0; i < 19; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < 19; j++) {
box[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0 ; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (box[i][j] == 0) continue;
for (int w = 0; w < 5; w++) {
if (check(i, j, box[i][j], w)) {
int midX = i + 1 + way[w][0] * 2;
int midY = j + 1 + way[w][1] * 2;
System.out.println(box[i][j]);
System.out.println(midX+" "+midY);
return;
}
}
}
}
System.out.println(0);
}
static boolean check(int x, int y, int color, int w) {
int cnt = 1; // check 함수 부를 때 초기 값이 그거라
while(cnt < 5) {
int nx = x + way[w][0];
int ny = y + way[w][1];
if (nx < 0 || nx >= 19 || ny < 0 || ny >= 19)return false;
if (box[nx][ny] == color) {
x = nx;
y = ny;
cnt += 1;
} else {
break;
}
}
return cnt == 5;
}
}
import java.io.*;
import java.util.*;
public class Main {
static int N, M;
static char[][] box;
static int[][] way = {{0,1}, {0,-1}, {1,0}, {-1,0}, {-1,-1}, {1,-1}, {1,1}, {-1,1}};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
box = new char[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
box[i][j] = line.charAt(j);
}
}
int res = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (box[i][j] == 'L') {
for(int w = 0; w < 8; w++) { // 가능한 방향
int cnt = 1;
int curX = i ; // 시작점
int curY = j ;
while (true) {
int nx = curX + way[w][0]; // 다음 위치
int ny = curY + way[w][1];
if(nx < 0 || nx >= N || ny < 0 || ny >= M)break;
if (box[nx][ny] != 'E') {
break;
}
cnt += 1;
curX = nx;
curY = ny;
}
if(cnt >= 3) {
res += 1;
}
}
}
}
}
System.out.println(res);
}
}
[코드트리] 벽 짚고 미로 탈출하기 (0) | 2024.10.11 |
---|---|
[코드트리] 다수의 객체 이동 (0) | 2024.10.10 |
[코드트리] 순위경쟁2 (0) | 2024.10.07 |
[BJ] 1182 : 부분수열의 합 (0) | 2024.10.05 |
[SWEA] 4408. 자기 방으로 돌아가기 (0) | 2024.10.01 |