cgy12306
[백준 BoJ] 14503 - 로봇 청소기 본문
// https://www.acmicpc.net/problem/14503
// 로봇 청소기
#include<iostream>
using namespace std;
int map[51][51];
bool visited[51][51];
int r, c, x, y, dir;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int cnt = 0;
void clear(int x, int y) {
visited[x][y] = true;
map[x][y] = 2;
cnt++;
}
bool turn_go() {
int nx = x + dx[dir];
int ny = y + dy[dir];
//cout << dir;
// 북
if (dir == 0) {
dir = 3;
}
// 동
else if (dir == 1) {
dir = 0;
}
else if (dir == 2) {
dir = 1;
}
else if (dir == 3) {
dir = 2;
}
if (map[nx][ny] == 0 && !visited[nx][ny]) {
x = nx;
y = ny;
return true;
}
else {
return false;
}
}
void direction() {
dir += 1;
if (dir == 4) dir = 0;
}
bool check() {
int nx, ny;
bool check_room = false;
for (int i = 0; i < 4; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (!(map[nx][ny] == 1 || visited[nx][ny])) {
// 청소 덜됨
check_room = true;
}
//if(map[nx][ny] == 0)
}
// 후진
if (!check_room) {
// 뒷 방향
//cout << dir <<"\n";
//dir += 1;
//direction();
nx = x + dx[dir];
ny = y + dy[dir];
if (map[nx][ny] == 1) {
return true;
}
else {
x = nx;
y = ny;
direction();
}
}
return false;
}
void print() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << map[i][j] << " ";
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> r >> c;
cin >> x >> y >> dir;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> map[i][j];
if (map[i][j] == 1) visited[i][j] = true;
}
}
do {
//cout << x << " " << y <<" " << dir << "\n";
if (x == 9 && y == 3) {}
if(!visited[x][y] && map[x][y] == 0) clear(x, y);
//print();
// 북 : 0, 동 : 1, 남 : 2, 서 : 3
if (turn_go()) continue;
if (check()) break;
} while (1);
cout << cnt;
}
- simulation
'Algorithm > C++' 카테고리의 다른 글
[백준 BoJ] 14499 - 주사위 굴리기 (0) | 2021.10.22 |
---|---|
[백준 BoJ] 13458 - 시험 감독 (0) | 2021.10.22 |
[백준 BoJ] 20055 - 컨베이버 벨트 위의 로봇 (0) | 2021.10.21 |
[백준 BoJ] 2947 - 나무 조각 (0) | 2021.10.15 |
[백준 BoJ] 17070 - 파이프 옮기기1 (0) | 2021.10.15 |
Comments