# 생각
주어진 조건 그대로 작성하면 된다
0일때 북쪽, 1은 동쪽, 2 남쪽, 3 서쪽이므로 dx, dy배열을 잘 확인해주고
2-c 조건인 후진에 유의해주면 된다
나머지는 기존 bfs문제처럼 해결 가능하다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define X first
#define Y second
#define rep(i,a,b) for(int i = a; i < b; i++)
#define ip1(a) cin >> a
#define ip2(a,b) cin >> a >> b
#define op1(a) cout << a << ' '
#define op2(a,b) cout << a << ' ' << b
#define op1l(a) cout << a << '\n'
#define op2l(a) cout << a << ' ' << b << '\n'
#define opl cout << '\n'
const int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 };
bool OOB(int x, int y, int n, int m) { return x < 0 or x >= n or y < 0 or y >= m; }
int n, m;
int graph[55][55], vis[55][55];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ip2(n, m);
int r, c, d;
cin >> r >> c >> d;
rep(i, 0, n) rep(j, 0, m) ip1(graph[i][j]);
int cnt = 0;
while (true)
{
if (vis[r][c] == 0)
{
vis[r][c] = 1;
cnt++;
}
bool flag = false;
rep(dir, 0, 4)
{
d = (d + 3) % 4;
int nx = r + dx[d], ny = c + dy[d];
if (OOB(nx, ny, n, m) or graph[nx][ny] == 1 or vis[nx][ny] == 1) continue;
r += dx[d];
c += dy[d];
flag = true;
break;
}
if (!flag)
{ // 후진
int nd = (d + 2) % 4;
int nx = r + dx[nd], ny = c + dy[nd];
if (OOB(nx, ny, n, m) or graph[nx][ny] == 1) break;
r += dx[nd];
c += dy[nd];
}
}
op1(cnt);
}
https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어
www.acmicpc.net
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 2309_일곱 난쟁이.cpp (0) | 2022.05.28 |
---|---|
[BOJ] 2146_다리 만들기 with cpp (0) | 2022.03.10 |
[BOJ] 9205_맥주 마시면서 걸어가기 with cpp (0) | 2022.03.09 |
[BOJ] 2573_빙산 with cpp (0) | 2022.03.08 |
[BOJ] 2644_촌수계산 with cpp (0) | 2022.03.07 |