https://upload.acmicpc.net/241319de-a09a-41ad-aad9-360e3cbbc391/
※. G가 하나일 때 -> 0
※. 떨어져 있는 G가 모일려면 화면 끝(모서리) 까지 가야함
※. G가 한 줄에 있을 때 -> 한쪽으로만 움직이면 됨 -> max(x) -1 (위쪽으로 모임) 과 N-min(x) (아래쪽으로 모임) 중 작은것
※. 나머지의 경우 G가 모이는 경우는 네 구석밖에 없음 -> 위에서 max(y)-1과 N-min(y)도 구해준다.
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
char[][] arr = new char[N][N];
int max = -1;
int mix = 1501;
int may = -1;
int miy = 1501;
for(int i = 0; i < N; i++) {
String str = br.readLine();
for(int j = 0; j < N; j++) {
if(str.charAt(j) == 'G') {
mix = Math.min(mix, i);
max = Math.max(max, i);
may = Math.max(may, j);
miy = Math.min(miy, j);
}
}
}
if(mix == max && may == miy)
System.out.println(0);
else if(mix == max){
int ans = Math.min(may, N-miy-1);
System.out.println(ans);
}
else if(may == miy) {
int ans = Math.min(max, N-mix-1);
System.out.println(ans);
}
else {
int ax = Math.min(max, N-mix-1);
int ay = Math.min(may, N-miy-1);
System.out.println(ax+ay);
}
}
}
'백준' 카테고리의 다른 글
[백준] 27113 잠입 [자바] (0) | 2023.01.11 |
---|---|
[백준] 27112 시간 외 근무 멈춰! [자바] (0) | 2023.01.11 |
[백준] 1647 도시 분할 계획 [자바] (0) | 2022.10.12 |
[백준] 10158 개미 [자바] (0) | 2022.09.08 |
[백준] 18353 병사 배치하기 [자바] (0) | 2022.09.08 |