https://www.acmicpc.net/problem/14503
#include <vector>
#include <iostream>
#include<queue>
using namespace std;
int main() {
int n, m, r, c, d;
int count = 0;
cin >> n >> m >> r >> c >> d;
vector<vector<int>> map(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}
int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
queue<vector<int>> q;
q.push({ r,c,d});
while (!q.empty()) {
int y = q.front()[0];
int x = q.front()[1];
int z = q.front()[2];
bool t = false;
q.pop();
if (map[y][x] == 0) {
map[y][x] = 2;
count++;
}
for (int i = 0; i < 4; i++) {
z--;
if (z < 0) z += 4;
int new_y = y + dir[z][0];
int new_x = x + dir[z][1];
if (map[new_y][new_x]==0) {
t = true;
q.push({ new_y, new_x, z });
break;
}
}
if (t) continue;
int new_y = y - dir[z][0];
int new_x = x - dir[z][1];
if ( map[new_y][new_x] != 1) {
q.push({ new_y, new_x, z });
}
else break;
}
cout << count;
}
bfs문제입니다. 2차원의 정사각형 맵에서 로봇청소기가 조건에 따라 움직일 때 청소한 칸의 개수를 구하면 됩니다.
기본적인 bfs알고리즘을 조금 변형해서 위의 조건대로 구현을 해줬습니다.