-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path86.cpp
More file actions
53 lines (44 loc) · 1.32 KB
/
Copy path86.cpp
File metadata and controls
53 lines (44 loc) · 1.32 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
int maxHeight, maxWidth, targetRow, targetCol, maxSteps;
int dx[4] = { 1, 0, 0, -1 };
int dy[4] = { 0, -1, 1, 0 };
char dir[4] = { 'd', 'l', 'r', 'u' };
string path = "";
bool found = false;
// 보드 내에 있는지 확인
bool InBounds(int x, int y) {
return x > 0 && x <= maxHeight && y > 0 && y <= maxWidth;
}
// 남은 이동 횟수로 도달 가능한지 확인
bool CanReach(int x, int y, int steps) {
return abs(x - targetRow) + abs(y - targetCol) <= steps;
}
// 경로 찾기
void Search(int x, int y, int steps, string currPath) {
if (steps == 0 && x == targetRow && y == targetCol) {
found = true;
path = currPath;
return;
}
for (int i = 0; i < 4; i++) {
int nextX = x + dx[i];
int nextY = y + dy[i];
if (!InBounds(nextX, nextY) || !CanReach(nextX, nextY, steps - 1)) continue;
Search(nextX, nextY, steps - 1, currPath + dir[i]);
if (found) return;
}
}
// 메인 함수
string solution(int n, int m, int x, int y, int r, int c, int k) {
maxHeight = n;
maxWidth = m;
targetRow = r;
targetCol = c;
maxSteps = k;
if (!CanReach(x, y, k) || ((abs(x - r) + abs(y - c)) % 2) != (k % 2)) return "impossible";
Search(x, y, k, "");
return path;
}