Recent Posts
Notice
No Rules Rules
로봇 청소기 (feat. 백준, 14503번) 본문
728x90
반응형
로봇 청소기
https://www.acmicpc.net/problem/14503
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <algorithm>
using namespace std;
int ans;
void left(register int& d) {
if (--d < 0)
d = 3;
}
void clean_position(register int x, register int y, int(*arr)[50]) {
if (arr[x][y] == 0)
++ans, arr[x][y] = 2;
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
ans = 0;
register int N, M, x, y, dir, dx[4]{ -1, 0, 1, 0 }, dy[4]{ 0, 1, 0, -1 }, arr[50][50];
cin >> N >> M >> x >> y >> dir;
for (register int n = 0, m; n < N; ++n)
for (m = 0; m < M; ++m)
cin >> arr[n][m];
while (1) {
// 1번 풀이
clean_position(x, y, arr);
// 2-1, 2-2번 풀이
register int d, nx, ny;
for (d = 0; d < 4; ++d) {
if (--dir < 0) dir = 3;
nx = x + dx[dir], ny = y + dy[dir];
if (0 <= nx && nx < N && 0 <= ny && ny < M && arr[nx][ny] == 0) {
x = nx, y = ny;
break;
}
}
// 2-3, 2-4번 풀이
if (d == 4) {
register int tmp_dir = (dir + 2) % 4;
nx = x + dx[tmp_dir], ny = y + dy[tmp_dir];
if (0 <= nx && nx < N && 0 <= ny && ny < M && arr[nx][ny] == 2)
x = nx, y = ny;
else // 2-4번 조건인 경우 종료
break;
}
}
cout << ans;
return 0;
}
// *&)*@*
- 문제로 주어진 위치부터 주어진 조건으로 해결하면 되는 구현 문제입니다.
- 주석과 같이 1번 풀이 | 2-1, 2-2번 풀이 | 2-3, 2-4번 풀이 를 순서대로 구현하면 됩니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
좋은 암호 (feat. 백준, 2061번) (2) | 2022.09.06 |
---|---|
소수 찾기 (feat. 프로그래머스, 42839번) (0) | 2022.09.05 |
연구소 (feat. 백준, 14502번) (2) | 2022.09.05 |
줄 세우기 (feat. 백준, 2252번) (0) | 2022.09.05 |
RGB거리 2 (feat. 백준, 17404번) (0) | 2022.09.05 |
Comments