Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.StringTokenizer;
- public class Main {
- static int N, M;
- static char[][] arr;
- static int[] dy = { 0, 1, -1 }, dx = { 1, 1, 1 };
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st = new StringTokenizer(br.readLine());
- N = Integer.parseInt(st.nextToken());
- M = Integer.parseInt(st.nextToken());
- arr = new char[N][M];
- int sy = 0, sx = 0;
- for (int i = 0; i < N; i++) {
- String input = br.readLine();
- for (int j = 0; j < M; j++) {
- char ch = input.charAt(j);
- if (ch == 'R') { // '토끼의 처음위치(y,x) 저장
- sy = i;
- sx = j;
- }
- arr[i][j] = ch;
- }
- }
- Queue<Loc> q = new LinkedList<Loc>();
- q.offer(new Loc(sy, sx, 0));
- int ret = -1;
- while (!q.isEmpty()) {
- Loc r = q.poll();
- for (int i = 0; i < 3; i++) {
- int gy = r.y + dy[i];
- int gx = r.x + dx[i];
- if (0 <= gy && gy < N && gx < M) {
- if (arr[gy][gx] == '#')
- continue;
- if (arr[gy][gx] == 'C') {
- q.add(new Loc(gy, gx, r.c + 1));
- } else if (arr[gy][gx] == '.') {
- q.add(new Loc(gy, gx, r.c));
- } else if (arr[gy][gx] == 'O') {
- ret = Math.max(ret, r.c);
- }
- }
- }
- }
- System.out.println(ret);
- br.close();
- }
- static class Loc {
- int y, x, c;
- Loc(int y, int x, int c) {
- this.y = y;
- this.x = x;
- this.c = c;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment