사용한 것
풀이 방법
- while 문을 돌며 다음과 같이 풀이한다.
- 방문하지 않았으면 방문 처리, 
answer 증가 
- 1 : 모든 방향을 다 돌았으면 (
isAllDirClean())
- 뒤로 움직일 수 있으면(
canBack()) 뒤로 후진 후 ct 초기화, 없으면 while 문 종료 
 
- 2 : 왼쪽 방향 깨끗하지 않으면 (
isLeftUnclean()) 
- 3 : 왼쪽 방향 깨끗하면
 
 
코드
public class Main {
    private static int n;
    private static int m;
    private static int[][] map;
    private static boolean[][] visited;
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        n = Integer.parseInt(line[0]);
        m = Integer.parseInt(line[1]);
        map = new int[n][m];
        visited = new boolean[n][m];
        line = br.readLine().split(" ");
        int initX = Integer.parseInt(line[0]);
        int initY = Integer.parseInt(line[1]);
        int initD = Integer.parseInt(line[2]);
        for (int x = 0; x < n; x++) {
            line = br.readLine().split(" ");
            for (int y = 0; y < m; y++) {
                map[x][y] = Integer.parseInt(line[y]);
            }
        }
        
        Robot robot = new Robot(initX, initY, initD);
        int answer = 0;
        while (true) {
            
            if (!visited[robot.x][robot.y]) {
                answer++;
                visited[robot.x][robot.y] = true;
            }
            
            if (isAllDirClean(robot)) { 
                if (!canBack(robot)) { 
                    break;
                }
                robot.back(); 
                robot.ct = 0;
            } else if (isLeftUnclean(robot)) { 
                robot.rotate();
                robot.move();
                robot.ct = 0;
            } else { 
                robot.rotate();
                robot.ct++;
            }
        }
        System.out.println(answer);
    }
    private static boolean isAllDirClean(Robot robot) {
        return robot.ct == 4;
    }
    private static boolean isLeftUnclean(Robot robot) {
        int nx = robot.x;
        int ny = robot.y;
        switch (robot.d) {
            case 0:
                ny--;
                break;
            case 1:
                nx--;
                break;
            case 2:
                ny++;
                break;
            default: 
                nx++;
                break;
        }
        if (!isOOB(nx, ny) && !visited[nx][ny]) {
            return true;
        }
        return false;
    }
    private static boolean canBack(Robot robot) {
        switch (robot.d) {
            case 0:
                return !isOOB(robot.x + 1, robot.y);
            case 1:
                return !isOOB(robot.x, robot.y - 1);
            case 2:
                return !isOOB(robot.x - 1, robot.y);
            default: 
                return !isOOB(robot.x, robot.y + 1);
        }
    }
    private static boolean isOOB(int x, int y) {
        if (map[x][y] == 1) {
            return true;
        }
        return false;
    }
}
class Robot {
    public int x;
    public int y;
    public int d; 
    public int ct;
    public Robot(int x, int y, int d) {
        this.x = x;
        this.y = y;
        this.d = d;
        ct = 0;
    }
    public void rotate() {
        switch (d) {
            case 0:
                d = 3;
                break;
            case 1:
                d = 0;
                break;
            case 2:
                d = 1;
                break;
            default: 
                d = 2;
                break;
        }
    }
    public void move() {
        switch (d) {
            case 0:
                x--;
                break;
            case 1:
                y++;
                break;
            case 2:
                x++;
                break;
            default: 
                y--;
                break;
        }
    }
    public void back() {
        switch (d) {
            case 0:
                x++;
                break;
            case 1:
                y--;
                break;
            case 2:
                x--;
                break;
            default: 
                y++;
                break;
        }
    }
}