삼성 SW 역량 테스트 기출문제

[C++] 삼성 SW 역량 테스트 기출문제 백준 14503번 - 로봇 청소기

코딩준우 2023. 7. 10. 23:13

 

 

https://www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$  둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽

www.acmicpc.net

 

 

 

#include <bits/stdc++.h>
// 삼성 SW 역량 테스트 기출문제  [C++] 백준

int n, m, r, c, d;
int map[50][50];
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};


int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
	std::cin >> n >> m >> r >> c >> d;

	for (int i = 0; i < n; ++i){
		for (int j = 0; j < m; ++j){
			std::cin >> map[i][j];
		}
	}

	std::queue<std::pair<int, int>> q;
	q.push({r, c});
	int ret = 0;

	while(!q.empty()){
		auto [y, x] = q.front();
		if (map[y][x] == 0) {
			++ret;
			map[y][x] = 2;
		}

		bool flag = false;
		for (int dir = 0; dir < 4; ++dir){
			int ny = y + dy[dir];
			int nx = x + dx[dir];
			if (map[ny][nx] != 0) continue;
			flag = true;
		}

		if (flag){
			d = (d + 3) % 4;
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (map[ny][nx] == 0){
				q.pop();
				q.push({ny, nx});
			}
		}
		else {
			int ny = y + dy[(d + 2) % 4];
			int nx = x + dx[(d + 2) % 4];
			if (map[ny][nx] == 1) break;
			q.pop();
			q.push({ny, nx});
		}
	}

	std::cout << ret << '\n';
	return 0;
}