forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.cpp
More file actions
103 lines (99 loc) Β· 3.96 KB
/
8.cpp
File metadata and controls
103 lines (99 loc) Β· 3.96 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int pos1X;
int pos1Y;
int pos2X;
int pos2Y;
Node(int pos1X, int pos1Y, int pos2X, int pos2Y) {
this->pos1X = pos1X;
this->pos1Y = pos1Y;
this->pos2X = pos2X;
this->pos2Y = pos2Y;
}
};
vector<Node> getNextPos(Node pos, vector<vector<int> > board) {
vector<Node> nextPos; // λ°ν κ²°κ³Ό (μ΄λ κ°λ₯ν μμΉλ€)
// (μ, ν, μ’, μ°)λ‘ μ΄λνλ κ²½μ°μ λν΄μ μ²λ¦¬
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
for (int i = 0; i < 4; i++) {
int pos1NextX = pos.pos1X + dx[i];
int pos1NextY = pos.pos1Y + dy[i];
int pos2NextX = pos.pos2X + dx[i];
int pos2NextY = pos.pos2Y + dy[i];
// μ΄λνκ³ μ νλ λ μΉΈμ΄ λͺ¨λ λΉμ΄ μλ€λ©΄
if (board[pos1NextX][pos1NextY] == 0 && board[pos2NextX][pos2NextY] == 0) {
nextPos.push_back(Node(pos1NextX, pos1NextY, pos2NextX, pos2NextY));
}
}
// νμ¬ λ‘λ΄μ΄ κ°λ‘λ‘ λμ¬ μλ κ²½μ°
int hor[] = {-1, 1};
if (pos.pos1X == pos.pos2X) {
for (int i = 0; i < 2; i++) { // μμͺ½μΌλ‘ νμ νκ±°λ, μλμͺ½μΌλ‘ νμ
// μμͺ½ νΉμ μλμͺ½ λ μΉΈμ΄ λͺ¨λ λΉμ΄ μλ€λ©΄
if (board[pos.pos1X + hor[i]][pos.pos1Y] == 0 && board[pos.pos2X + hor[i]][pos.pos2Y] == 0) {
nextPos.push_back(Node(pos.pos1X, pos.pos1Y, pos.pos1X + hor[i], pos.pos1Y));
nextPos.push_back(Node(pos.pos2X, pos.pos2Y, pos.pos2X + hor[i], pos.pos2Y));
}
}
}
// νμ¬ λ‘λ΄μ΄ μΈλ‘λ‘ λμ¬ μλ κ²½μ°
int ver[] = {-1, 1};
if (pos.pos1Y == pos.pos2Y) {
for (int i = 0; i < 2; i++) { // μΌμͺ½μΌλ‘ νμ νκ±°λ, μ€λ₯Έμͺ½μΌλ‘ νμ
// μΌμͺ½ νΉμ μ€λ₯Έμͺ½ λ μΉΈμ΄ λͺ¨λ λΉμ΄ μλ€λ©΄
if (board[pos.pos1X][pos.pos1Y + ver[i]] == 0 && board[pos.pos2X][pos.pos2Y + ver[i]] == 0) {
nextPos.push_back(Node(pos.pos1X, pos.pos1Y, pos.pos1X, pos.pos1Y + ver[i]));
nextPos.push_back(Node(pos.pos2X, pos.pos2Y, pos.pos2X, pos.pos2Y + ver[i]));
}
}
}
// νμ¬ μμΉμμ μ΄λν μ μλ μμΉλ₯Ό λ°ν
return nextPos;
}
int solution(vector<vector<int> > board) {
// λ§΅μ μΈκ³½μ λ²½μ λλ ννλ‘ λ§΅ λ³ν
int n = board.size();
vector<vector<int> > newBoard(n + 2, vector<int>(n + 2, 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newBoard[i + 1][j + 1] = board[i][j];
}
}
// λλΉ μ°μ νμ(BFS) μν
queue<pair<Node, int> > q;
vector<Node> visited;
Node pos = Node(1, 1, 1, 2); // μμ μμΉ μ€μ
q.push({pos, 0}); // νμ μ½μ
ν λ€μ
visited.push_back(pos); // λ°©λ¬Έ μ²λ¦¬
// νκ° λΉ λκΉμ§ λ°λ³΅
while (!q.empty()) {
Node pos = q.front().first;
int cost = q.front().second;
q.pop();
// (n, n) μμΉμ λ‘λ΄μ΄ λλ¬νλ€λ©΄, μ΅λ¨ 거리μ΄λ―λ‘ λ°ν
if ((pos.pos1X == n && pos.pos1Y == n) || (pos.pos2X == n && pos.pos2Y == n)) {
return cost;
}
// νμ¬ μμΉμμ μ΄λν μ μλ μμΉ νμΈ
vector<Node> nextPos = getNextPos(pos, newBoard);
for (int i = 0; i < nextPos.size(); i++) {
// μμ§ λ°©λ¬Ένμ§ μμ μμΉλΌλ©΄ νμ μ½μ
νκ³ λ°©λ¬Έ μ²λ¦¬
bool check = true;
Node pos = nextPos[i];
for (int j = 0; j < visited.size(); j++) {
if (pos.pos1X == visited[j].pos1X && pos.pos1Y == visited[j].pos1Y && pos.pos2X == visited[j].pos2X && pos.pos2Y == visited[j].pos2Y) {
check = false;
break;
}
}
if (check) {
q.push({pos, cost + 1});
visited.push_back(pos);
}
}
}
return 0;
}