-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskill_tree.cpp
More file actions
118 lines (82 loc) · 1.87 KB
/
Copy pathskill_tree.cpp
File metadata and controls
118 lines (82 loc) · 1.87 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// https://programmers.co.kr/learn/courses/30/lessons/49993
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
queue<vector<int>> q;
int BFS(vector<vector<int>> maps, int **boolarr) {
vector<int>a = q.front();
int x = a.at(0);
int y = a.at(1);
int d = a.at(2);
boolarr[0][0] = 1;
while (!q.empty()) {
a = q.front();
x = a.at(0);
y = a.at(1);
d = a.at(2);
if (x == maps.size() - 1 && y == maps.at(0).size() - 1) {
printf("%d", d);
return d;
}
if (x + 1 < maps.size() && maps[x + 1][y] == 1 && boolarr[x + 1][y] == 0) {
boolarr[x + 1][y] = 1;
q.push({ x + 1, y,d + 1 });
}
if (y + 1 < maps.at(0).size() && maps[x][y + 1] == 1 && boolarr[x][y + 1] == 0) {
boolarr[x][y + 1] = 1;
q.push({ x , y + 1,d + 1 });
}
if (x - 1 >= 0 && maps[x - 1][y] == 1 && boolarr[x - 1][y] == 0) {
boolarr[x - 1][y] = 1;
q.push({ x - 1, y,d + 1 });
}
if (y - 1 >= 0 && maps[x][y - 1] == 1 && boolarr[x][y - 1] == 0) {
boolarr[x][y - 1] = 1;
q.push({ x, y - 1,d + 1 });
}
/*for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", boolarr[i][j]);
}
printf("\n");
}
*/
q.pop();
}
printf("%d", -1);
return -1;
}
int solution(vector<vector<int>> maps) {
vector<int> v1 = { 0,0,1 };
q.push(v1);
int **boolarr;
boolarr = (int**)calloc(sizeof(int*), maps.size());
for (int i = 0; i < maps.size(); i++) {
boolarr[i] = (int*)calloc(sizeof(int), maps.at(0).size());
}
int answer = BFS(maps, boolarr);
return answer;
}
int main()
{
vector<vector<int>> maze;
int n;
int value;
cin >> n;
for (int i = 0; i < n; i++) {
vector<int> tmp_maze;
for (int i = 0; i < n; i++) {
cin >> value;
tmp_maze.push_back(value);
}
maze.push_back(tmp_maze);
}
solution(maze);
return 0;
}