https://www.acmicpc.net/problem/17267
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static class Status implements Comparable<Status>{
public int x, y, L, R;
public Status(int x, int y, int l, int r) {
this.x = x;
this.y = y;
L = l;
R = r;
}
@Override
public int compareTo(Status target) {
return (L+R) <= (target.L + target.R) ? 1 : -1;
}
}
public static int sX, sY, N, M, L, R, result;
public static int[][] map = new int[1001][1001];
public static boolean[][] chk = new boolean[1001][1001];
public static PriorityQueue<Status> queue = new PriorityQueue<>();
public static int[] dx = {0,1,0,-1};
public static int[] dy = {1,0,-1,0};
public static void solve() {
//시작 지점 체크
result = 1;
chk[sX][sY] = true;
queue.add(new Status(sX, sY, L, R));
while(!queue.isEmpty()) {
Status curStatus = queue.poll();
for (int d = 0; d < 4; d++) {
int X = curStatus.x + dx[d];
int Y = curStatus.y + dy[d];
if (X < 0 || Y < 0 || X >= N || Y >= M || chk[X][Y] == true || map[X][Y] == 1) continue;
//우측으로 갈 때
if (d == 0) {
if (curStatus.R != 0) {
chk[X][Y] = true;
result++;
queue.add(new Status(X, Y, curStatus.L, curStatus.R-1));
}
}
//좌측으로 갈 때
else if (d == 2) {
if (curStatus.L != 0) {
chk[X][Y] = true;
result++;
queue.add(new Status(X, Y, curStatus.L-1, curStatus.R));
}
}
//위, 아래로 갈 때
else {
chk[X][Y] = true;
result++;
queue.add(new Status(X, Y, curStatus.L, curStatus.R));
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt(); M = sc.nextInt(); L = sc.nextInt(); R = sc.nextInt();
for (int i = 0; i < N; i++) {
String input = sc.next();
for (int j = 0; j < M; j++) {
map[i][j] = Character.getNumericValue(input.charAt(j));
if (map[i][j] == 2) {
sX = i; sY = j;
map[i][j] = 0;
}
}
}
solve();
System.out.println(result);
}
}