#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
 
int row, col;
int map[55][55];
int visited[55][55];
int x, y, dir;
int cleanCnt;
 
int dx[4= {-1010}; // 북 동 남 서 
int dy[4= {010-1}; // 북 동 남 서
 
int main(void)
{
//    freopen("B14503_input.txt", "r", stdin);
    
    cin >> row >> col;
    
    cin >> x >> y >> dir;
    
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cin >> map[i][j];
        }
    }
    
    bool clean = true;
    int all_cleanCnt = 0;
    while(1)
    {    
        // c. 네 방향 모두 청소가 이미 되어있거나 벽인 경우에는
        if(all_cleanCnt == 4)
        {
            all_cleanCnt = 0;
            
            int nDir = (dir+2) % 4;
            int nx = x+dx[nDir];
            int ny = y+dy[nDir];
            
            // d. 뒤쪽 방향이 벽이라 후진도 할 수 없는 경우에는 작동을 멈춘다.
            if(map[nx][ny] == 1)
            {
                break;
            }
            // c. 바라보는 방향을 유지한 채로 한 칸 후진을 하고 2번으로 돌아간다.
            else
            {
                x = nx;
                y = ny;
                
                continue;
            }    
        }
        
        // 1. 현재 위치를 청소한다.    
        if(clean)
        {
            all_cleanCnt = 0;
            
            visited[x][y] = 1;
            cleanCnt++;
            clean = false;
        }
        
        // 2. 현재 위치에서 현재 방향을 기준으로 왼쪽방향부터
        int tempDir = (dir+3) % 4;
        int xpos = x+dx[tempDir];
        int ypos = y+dy[tempDir];
        
        // a. 왼쪽 방향에 아직 청소하지 않은 공간이 존재한다면, 그 방향으로 회전한 다음 한 칸을 전진하고 1번부터 진행한다.
        if(visited[xpos][ypos] == 0 && map[xpos][ypos] == 0)
        {
            dir = tempDir;
            x = xpos;
            y = ypos;
            clean = true;
            
            continue;
        }
        
        // b. 왼쪽 방향에 청소할 공간이 없다면, 그 방향으로 회전하고 2번으로 돌아간다.
        if(visited[xpos][ypos] == 1 || map[xpos][ypos] == 1)
        {            
            all_cleanCnt++;
            
            dir = tempDir;
            continue;
        }    
    }
    
    cout << cleanCnt;
    
    return 0;
}
cs

+ Recent posts