Muscardinus
로봇청소기 본문
728x90
https://www.acmicpc.net/problem/14503
#include <iostream>
#include <queue>
using namespace std;
struct Robot {
int rx, ry, rd;
Robot(int y, int x, int d) {
ry = y; rx = x; rd = d;
}
};
int n, m;
int board[51][51];
int result = 0;
int dy[] = {-1,0,1,0};
int dx[] = {0,1,0,-1};
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
int rx, ry, rd;
cin >> ry >> rx >> rd;
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
cin >> board[y][x];
}
}
queue<Robot> Q;
Q.push(Robot(ry,rx,rd));
while (!Q.empty()) {
int nowY = Q.front().ry;
int nowX = Q.front().rx;
int nowD = Q.front().rd;
if (board[nowY][nowX] == 0) {
board[nowY][nowX] = 2;
result++;
}
Q.pop();
for (int i = 0; i < 4; i++) {
int nextD = (nowD + 3 - i) % 4;
int nextY = nowY + dy[nextD];
int nextX = nowX + dx[nextD];
if (nextY < 0 || nextY >= n || nextX < 0 || nextX >= m || board[nextY][nextX] != 0) continue;
Q.push(Robot(nextY,nextX,nextD));
break;
}
if (Q.empty()) {
int nextD = nowD;
int nextY = nowY + dy[(nextD + 2) % 4];
int nextX = nowX + dx[(nextD + 2) % 4];
if (nextY < 0 || nextY >= n || nextX < 0 || nextX >= m || board[nextY][nextX] == 1) break;
Q.push(Robot(nextY, nextX, nextD));
}
}
cout << result<<"\n";
return 0;
}
728x90
Comments