728x90
๋ฌธ์
https://www.acmicpc.net/problem/14503
์ฝ๋
#include <iostream>
using namespace std;
void CleanArea(int, int, int);
const int direction[4][2] = { {-1,0},{0,1},{1,0},{0,-1} }; //๋ถ, ๋, ๋จ, ์
int map[51][51];
int height, width;
int clean_count;
int main() {
cin >> height >> width;
int robot_x = 0, robot_y = 0, robot_direction = 0;
cin >> robot_x >> robot_y >> robot_direction;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
cin >> map[y][x];
}
}
CleanArea(robot_x, robot_y, robot_direction);
cout << clean_count;
}
void CleanArea(int robot_x, int robot_y, int robot_direction) {
//1. ํ์ฌ ์์น๋ฅผ ์ฒญ์ํ๋ค.
if (map[robot_x][robot_y] == 0) {
map[robot_x][robot_y] = 2;
clean_count += 1;
}
//2. ํ์ฌ ์์น์์ ํ์ฌ ๋ฐฉํฅ์ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ๋ฐฉํฅ๋ถํฐ ์ฐจ๋ก๋๋ก ํ์์ ์งํํ๋ค.
for (int i = 0; i < 4; i++) {
robot_direction = (robot_direction + 3) % 4;
int next_x = robot_x + direction[robot_direction][0];
int next_y = robot_y + direction[robot_direction][1];
//a. ์ผ์ชฝ ๋ฐฉํฅ์ ์์ง ์ฒญ์ํ์ง ์์ ๊ณต๊ฐ์ด ์กด์ฌํ๋ค๋ฉด, ๊ทธ ๋ฐฉํฅ์ผ๋ก ํ์ ํ ๋ค์ ํ ์นธ์ ์ ์งํ๊ณ 1๋ฒ๋ถํฐ ์งํํ๋ค.
if (map[next_x][next_y] == 0) {
CleanArea(next_x, next_y, robot_direction);
return; //โ
โ
โ
๋์์์ ๋ ์ํ์ข์ฐ๋ฅผ ๋ณด๋๊ฒ ์๋๋ผ ๋น ์ ธ๋์์ผ ํจ
}//b. ์ผ์ชฝ ๋ฐฉํฅ์ ์ฒญ์ํ ๊ณต๊ฐ์ด ์๋ค๋ฉด, ๊ทธ ๋ฐฉํฅ์ผ๋ก ํ์ ํ๊ณ 2๋ฒ์ผ๋ก ๋์๊ฐ๋ค.
}
//c. ๋ค ๋ฐฉํฅ ๋ชจ๋ ์ฒญ์๊ฐ ์ด๋ฏธ ๋์ด์๊ฑฐ๋ ๋ฒฝ์ธ ๊ฒฝ์ฐ์๋ ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ ์ ์งํ ์ฑ๋ก ํ ์นธ ํ์ง์ ํ๊ณ 2๋ฒ์ผ๋ก ๋์๊ฐ๋ค.
int back_x = robot_x - direction[robot_direction][0];
int back_y = robot_y - direction[robot_direction][1];
if (map[back_x][back_y] != 1) {
CleanArea(back_x, back_y, robot_direction);
}
//d. ๋ค ๋ฐฉํฅ ๋ชจ๋ ์ฒญ์๊ฐ ์ด๋ฏธ ๋์ด์๊ฑฐ๋ ๋ฒฝ์ด๋ฉด์, ๋ค์ชฝ ๋ฐฉํฅ์ด ๋ฒฝ์ด๋ผ ํ์ง๋ ํ ์ ์๋ ๊ฒฝ์ฐ์๋ ์๋์ ๋ฉ์ถ๋ค.
return;
}
728x90
'๐ Coding Test Study > Algorithm Problem' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++][BAEKJOON][DP] 9456๋ฒ ์คํฐ์ปค (0) | 2021.08.22 |
---|---|
[C++][ํ๋ก๊ทธ๋๋จธ์ค] ๊ดํธ ๋ณํ (0) | 2021.08.22 |
[C++][Baekjoon][DP] 1309๋ฒ ๋๋ฌผ์ (0) | 2021.08.22 |
[C++][Baekjoon] 16974๋ฒ ์์ธ ์งํ์ฒ 2ํธ์ (0) | 2021.08.22 |
[C++][BAEKJOON][Dijkstra Alg] 1261๋ฒ ์๊ณ ์คํ (0) | 2021.08.22 |