Home BOJ. Robot Vaccum Cleaner (14503)
Post
Cancel

BOJ. Robot Vaccum Cleaner (14503)

[Link] https://www.acmicpc.net/problem/14503


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 987654321;
int n, m, cnt;
int ci, cj, dir;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
int map[50][50];
bool visit[50][50];

void solve() {
  cnt = 1;

  while(true) {
    bool breakloop = false;
    visit[ci][cj] = true;
    for(int i = 0; i < 4; i++) {
      if(--dir < 0) dir = 3;
      int ii = ci + di[dir], jj = cj + dj[dir];
      if(ii < 0 || jj < 0 || ii >= n || jj >= m || visit[ii][jj] || map[ii][jj]) continue;
      visit[ii][jj] = true;
      cnt++;
      ci = ii, cj = jj;
      breakloop = true;
      break;
    }
    if(breakloop) continue;
    int back = dir - 2; if(back < 0) back += 4;
    int ii = ci + di[back], jj = cj + dj[back];
    if(ii < 0 || jj < 0 || ii >= n || jj >= m || map[ii][jj]) return;
    ci = ii, cj = jj;
    if(!map[ci][cj] && !visit[ci][cj]) {
      visit[ci][cj] = true;
      cnt++;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  cin >> n >> m;
  cin >> ci >> cj >> dir;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) cin >> map[i][j];
  }
  solve();
  cout << cnt;
  return 0;
}
This post is licensed under CC BY 4.0 by the author.