#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = 51;
int n, m, r, c, d;
int map[MAX][MAX],visited[MAX][MAX];
bool rangecheck(int i, int j) {
if (i >= 0 && i < n && j >= 0 && j < m)
return true;
else
return false;
}
void vacuum(int i, int j, int dir) {
visited[i][j] = 1;
for (int k = 0; k < 4; k++) {
if (dir == 0) {
dir = 3;
if (map[i][j - 1] == 0 && visited[i][j - 1] == -1 && rangecheck(i,j-1)) {
vacuum(i, j - 1, dir);
return;
}
continue;
}
else if (dir == 1) {
dir = 0;
if (map[i - 1][j] == 0 && visited[i - 1][j] == -1 && rangecheck(i-1, j)) {
vacuum(i - 1, j, dir);
return;
}
continue;
}
else if (dir == 2) {
dir = 1;
if (map[i][j + 1] == 0 && visited[i][j + 1] == -1 && rangecheck(i, j + 1)) {
vacuum(i, j + 1, dir);
return;
}
continue;
}
else if (dir == 3) {
dir = 2;
if (map[i + 1][j] == 0 && visited[i + 1][j] == -1 && rangecheck(i+1, j)) {
vacuum(i + 1, j, dir);
return;
}
continue;
}
}
if (dir == 0) {
if (map[i + 1][j] == 0 && rangecheck(i + 1, j))
vacuum(i + 1, j, dir);
else
return;
}
else if (dir == 1) {
if (map[i][j - 1] == 0 && rangecheck(i, j-1))
vacuum(i, j - 1, dir);
else
return;
}
else if (dir == 2) {
if (map[i - 1][j] == 0 && rangecheck(i - 1, j))
vacuum(i - 1, j, dir);
else
return;
}
else if (dir == 3) {
if (map[i][j + 1] == 0 && rangecheck(i, j+1))
vacuum(i, j + 1, dir);
else
return;
}
return;
}
int main(void) {
int answer = 0;
cin >> n >> m >> r >> c >> d;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cin >> map[i][j];
}
memset(visited, -1, sizeof(visited));
vacuum(r, c, d);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (visited[i][j] == 1)
answer++;
}
}
cout << answer << endl;
return 0;
}
'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글
백준 2164번 C++ (0) | 2019.08.28 |
---|---|
백준 15685번 C++ (0) | 2019.08.28 |
백준 2455번 C++ (0) | 2019.08.26 |
백준 14499번 C++ (0) | 2019.04.07 |
백준 3190 C++ (0) | 2019.04.07 |