문제풀기 : [백준14503 로봇청소기]
package sw;
import java.io.*;
public class Main14503_로봇청소기 {
static int N,M,r,c,d,res;
static boolean flag;
static int[][] map;
static int[][] dir = {{-1,0},{0,1},{1,0},{0,-1}};
static boolean check(int x, int y) {
if(x>=0 && x<map.length && y>=0 && y<map[x].length)
return true;
else return false;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split(" ");
N = Integer.parseInt(input[0]);
M = Integer.parseInt(input[1]);
map = new int[N][M];
input = br.readLine().trim().split(" ");
r = Integer.parseInt(input[0]);
c = Integer.parseInt(input[1]);
d = Integer.parseInt(input[2]);
for(int i=0; i<N; i++) {
input = br.readLine().trim().split(" ");
for(int j=0; j<M; j++) {
map[i][j] = Integer.parseInt(input[j]);
}
}
play();
System.out.println(res);
}
static int dx,dy, tmpD;
static void play() {
if(flag) return;
if(map[r][c] == 0) {
res++;
map[r][c] = -1;
}
for(int dd=1; dd<=4; dd++) {
tmpD = (4+d-dd)%4; // 현재방향 기준으로 좌,하,우,상 해주기 위함.
dx = r + dir[tmpD][0];
dy = c + dir[tmpD][1];
if(!check(dx,dy) || map[dx][dy] == 1 || map[dx][dy] == -1) {
continue;
}
else if(check(dx,dy) && map[dx][dy] == 0) {
r = dx;
c = dy;
d = tmpD;
play();
return;
}
}
tmpD = (d+2)%4;
dx = r + dir[tmpD][0];
dy = c + dir[tmpD][1];
if(check(dx,dy) && map[dx][dy] != 1) {
r = dx;
c = dy;
play();
}else {
flag = true;
}
}
static int direct(char c) {
if(c == 'R') {
return (d+1)%4;
}else {
return (d+3)%4;
}
}
static void print() {
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
}
PREVIOUS[삼성기출]백준15684 - 사다리 조작