Skip to content

Instantly share code, notes, and snippets.

@niklasjang
Created April 30, 2020 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklasjang/c8ebffac58fb27ce645a55e76ebb4fe0 to your computer and use it in GitHub Desktop.
Save niklasjang/c8ebffac58fb27ce645a55e76ebb4fe0 to your computer and use it in GitHub Desktop.
[PS][기출문제][삼성]/[BOJ][14503][로봇청소기]
#include <iostream>
using namespace std;
int n, m, dir;
int map[50][50];
bool visited[50][50];
int dx[4] = { -1, 0, 1 , 0 };
int dy[4] = { 0,1,0,-1 };
int nextDir[4] = { 3,0,1,2 };
pair<int, int> curr;
int ans = 0;
bool inRange(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
void dfs(int x, int y) {
if (map[x][y] != 2) {
map[x][y] = 2;
ans++;
}
int k = 0;
int nd = 0, nx = 0, ny = 0;
for (k = 0; k < 4; k++) {
nd = nextDir[dir];
nx = x + dx[nd];
ny = y + dy[nd];
if (!inRange(nx, ny)) continue;
if (map[nx][ny] == 0) {
dir = nd;
return dfs(nx, ny);
}
else {
dir = nd;
continue;
}
}
if (inRange(x - dx[dir], y - dy[dir]) &&
map[x - dx[dir]][y - dy[dir]] != 1)
return dfs(x - dx[dir], y - dy[dir]);
else return;
}
int main (void) {
cin >> n >> m;
int i = 0, j = 0;
cin >> curr.first >> curr.second;
cin >> dir;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> map[i][j];
}
}
dfs(curr.first, curr.second);
cout << ans << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment