esfjge   2년 전

import java.util.*;
import java.util.regex.*;

public class Main {
	public int solution(Marble marble, int[][] map, Queue<Marble> queue, boolean[][][][] visited) {
		int answer = -1;
		queue.add(new Marble(marble.rx, marble.ry, marble.bx, marble.by, 1));
		visited[marble.rx][marble.ry][marble.bx][marble.by] = true;
		
		while (!queue.isEmpty()) {
			Marble here = queue.poll();
			for (int i = 0; i < 4; i++) {
				int rx = here.rx;
				int ry = here.ry;
				int bx = here.bx;
				int by = here.by;
				int cnt = here.cnt;
				int redcount = 0;
				int bluecount = 0;
				if (cnt > 10) return -1;
				if (i == 0  && map[rx][ry-1] != '#') {
					while (map[rx][ry-1] != '#') {
						ry -= 1;
						redcount +=1;
						if (map[rx][ry] == 'O') break;
					}
					while (map[bx][by-1] != '#') {
						by -= 1;
						bluecount +=1;
						if (map[bx][by] == 'O') break;
					}
					if (map[bx][by] == 'O') continue;
					if (map[rx][ry] == 'O') return here.cnt;
					if (rx == bx && ry == by) {
						if (redcount < bluecount) by += 1;
						else ry +=1;
					}
					if (visited[rx][ry][bx][by] != true) queue.add(new Marble(rx, ry, bx, by, cnt+1));
				} else if (i == 1 && map[rx][ry+1] != '#') {
					while (map[rx][ry+1] != '#') {
						ry += 1;
						redcount+=1;
						if (map[rx][ry] == 'O') break;
					}
					while (map[bx][by+1] != '#') {
						by += 1;
						bluecount+=1;
						if (map[bx][by] == 'O') break;
					}
					if (map[bx][by] == 'O') continue;
					if (map[rx][ry] == 'O') return here.cnt;
					if (rx == bx && ry == by) {
						if (redcount < bluecount) by -=1;
						else ry -=1;
					}
					if (visited[rx][ry][bx][by] != true) queue.add(new Marble(rx, ry, bx, by, cnt+1));
				} else if (i == 2 && map[rx+1][ry] != '#') {
					while (map[rx+1][ry] != '#') {
						rx += 1;
						redcount+=1;
						if (map[rx][ry] == 'O') break;
					}
					while (map[bx+1][by] != '#') {
						bx += 1;
						bluecount+=1;
						if (map[bx][by] == 'O') break;
					}
					if (map[bx][by] == 'O') continue;
					if (map[rx][ry] == 'O') return here.cnt;
					if (rx == bx && ry == by) {
						if (redcount < bluecount) bx -=1;
						else rx -=1;
					}
					if (visited[rx][ry][bx][by] != true) queue.add(new Marble(rx, ry, bx, by, cnt+1));
				} else if (i == 3 && map[rx-1][ry] != '#') {
					while (map[rx-1][ry] != '#') {
						rx -= 1;
						redcount+=1;
						if (map[rx][ry] == 'O') break;
					}
					while (map[bx-1][by] != '#') {
						bx -= 1;
						bluecount+=1;
						if (map[bx][by] == 'O') break;
					}
					if (map[bx][by] == 'O') continue;
					if (map[rx][ry] == 'O') return here.cnt;
					if (rx == bx && ry == by) {
						if (redcount < bluecount) bx +=1;
						else rx +=1;
					}
					if (visited[rx][ry][bx][by] != true) queue.add(new Marble(rx, ry, bx, by, cnt+1));
				}
			}
		}
		
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main T = new Main();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int bx = 0;
		int by = 0;
		int rx = 0;
		int ry = 0;
		int[][] arr = new int[n][m];
		for (int i = 0; i < n; i++) {
			String x = sc.next();
			for (int j = 0; j < m; j++) {
				arr[i][j] = x.charAt(j);
				if (x.charAt(j) == 'B') {
					bx = i;
					by = j;
				}
				if (x.charAt(j) == 'R') {
					rx = i;
					ry = j;
				}
			}
		}
		boolean[][][][] visited = new boolean[n][m][n][m];
		Marble marble = new Marble(rx, ry, bx, by, 0);
		Queue<Marble> queue = new LinkedList<>();
		System.out.println(T.solution(marble, arr, queue, visited));
	}
}

class Marble {
	int rx;
	int ry;
	int bx;
	int by;
	int cnt;
	
	Marble(int rx, int ry, int bx, int by, int cnt) {
		this.rx = rx;
		this.ry = ry;
		this.bx = bx;
		this.by = by;
		this.cnt = cnt;
	}
}

결과가 10인 반례에서 11을 리턴하며 실패합니다. 다른 반례와 예제는 통과하였습니다. 어디가 잘못되었는지, 보다 직접적인 반례는 없는지 궁금합니다. 

siyamaki   2년 전

반례입니다

댓글을 작성하려면 로그인해야 합니다.