Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 이득우
- 웅진씽크빅
- 구현
- DFS
- 유니티
- 운영체제
- 게임개발공모전
- fsm
- upper_bound
- 안드로이드
- 언리얼
- 너비우선탐색
- lower_bound
- 유한상태기계
- c#
- 개발일지
- 게임개발
- 알고리즘
- BFS
- 백준
- 이분탐색
- 프로그래머스
- 인프런
- unreal
- c++
- UI 자동화
- unity
- 재귀
- 시리얼라이제이션
- binary_search
Archives
- Today
- Total
초고교급 희망
[백준][C++]14503번 로봇 청소기 본문
728x90
#include <iostream>
using namespace std;
int n, m;
int room[51][51];
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int start_x, start_y, start_d;
int cnt = 0;
int find_direction(int _d)
{
if (_d == 0)
{
return 3;
}
else
{
return _d - 1;
}
}
void clean(int x, int y, int d)
{
if (room[x][y] == 0)
{
room[x][y] = 2;
cnt++;
}
for (int i = 0; i < 4; i++)
{
int nd = find_direction(d);
int nx = x + dx[nd];
int ny = y + dy[nd];
if (room[nx][ny] == 0)
{
clean(nx, ny, nd);
return;
}
d = nd;
}
int back_d = find_direction(find_direction(d));
int nx = x + dx[back_d];
int ny = y + dy[back_d];
if (room[nx][ny] != 1)
{
clean(nx, ny, d);
}
}
int main()
{
cin >> n >> m;
cin >> start_x >> start_y >> start_d;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> room[i][j];
}
}
clean(start_x, start_y, start_d);
cout << cnt << endl;
return 0;
}
find_direction으로 방향을 90도씩 바꿉니다.
후진할 때는 180도 회전하는 것이므로 두 번 실행합니다.
청소가 끝난 방은 2로 두었습니다.
728x90
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준][C++]3190번 뱀 (0) | 2023.06.21 |
---|---|
[백준][C++]7490번 0 만들기 (0) | 2023.06.05 |
[백준][C++]2563번 색종이 (1) | 2023.04.18 |
[백준][C++]11404번 플로이드 (0) | 2022.09.27 |
[백준][C++]2805번 나무 자르기 (0) | 2022.05.16 |