*visited를 3차원(x,y,dir)으로 설정해서 지금 좌표와 가르키는 방향을 모두 저장한다.
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
119
120
|
#include <iostream>
#include <vector>
#include <queue>
#define INF 987654321
using namespace std;
int n, m, ans = INF;
int map[101][101];
typedef pair<int, int> pp;
typedef pair<pp, int> ppi;
typedef pair<pp, pp> pppp;
ppi start, dest;
bool visited[101][101][5];
int dx[5] = {0, 0, 0, 1, -1}, dy[5] = {0, 1, -1, 0, 0};
queue<pppp> q;
bool check(int x, int y)
{
if (x < 0 || x >= m || y < 0 || y >= n || map[x][y] == 1)
return false;
return true;
}
void bfs()
{
//시작 위치에서 도착위치 + 방향까지 맞춘 시점이 끝
while (!q.empty())
{
int size = q.size();
while (size--)
{
q.pop();
{
ans = ans > cnt ? cnt : ans;
continue;
}
//각 방향으로 나아가려면 그 방향으로 전환을 먼저 해야한다.
// cout << "go before " << x << " " << y << " " << dir << '\n';
int nx = x;
int ny = y;
for (int j = 1; j <= 3; j++)
{
nx = x + dx[dir] * j;
ny = y + dy[dir] * j;
if (check(nx, ny))
{
if (!visited[nx][ny][dir])
{
visited[nx][ny][dir] = true;
// cout << "go " << j << " : " << nx << " " << ny << " " << i << " " << cnt+1 << '\n';
q.push(pppp(pp(nx, ny), pp(dir, cnt + 1)));
}
}
else
break;
}
int n_dir = 5 - dir;
if (!visited[x][y][n_dir])
{
visited[x][y][n_dir] = true;
q.push(pppp(pp(x, y), pp(n_dir, cnt + 1)));
}
n_dir = dir <= 2 ? dir + 2 : dir - 2;
if (!visited[x][y][n_dir])
{
visited[x][y][n_dir] = true;
q.push(pppp(pp(x, y), pp(n_dir, cnt + 1)));
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> m >> n;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> map[i][j];
}
}
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
q.push(pppp(pp(a, b), pp(c, 0)));
visited[a][b][c] = true;
cin >> a >> b >> c;
a--;
b--;
dest = ppi(pp(a, b), c);
bfs();
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
도움이 된 TC
4 2
0 0
0 0
1 0
0 0
1 1 3
4 1 3