No Rules Rules

로봇 청소기 (feat. 백준, 14503번) 본문

생활/코테

로봇 청소기 (feat. 백준, 14503번)

개발하는 완두콩 2022. 9. 5. 20:55
728x90
반응형

로봇 청소기
https://www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

www.acmicpc.net

 

반응형

 

// 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-1, 2-2번 풀이 | 2-3, 2-4번 풀이 를 순서대로 구현하면 됩니다.
728x90
반응형
Comments