-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16509.cpp
More file actions
102 lines (90 loc) · 1.57 KB
/
16509.cpp
File metadata and controls
102 lines (90 loc) · 1.57 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
// 16509. 장군
// 2019.09.01
// BFS
#include<iostream>
#include<queue>
using namespace std;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
int dx2[4][2] = { {-1,-1},{-1,1},{1,1},{-1,1} };
int dy2[4][2] = { {-1,1},{1,1},{-1,1},{-1,-1} };
int visit[11][10];
// 범위를 벗어낫는지 체크하는 함수
bool check(int x, int y)
{
if (x < 0 || y < 0 || x >= 10 || y >= 9)
{
return true;
}
return false;
}
int main()
{
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 10; j++)
{
visit[i][j] = -1;
}
}
queue<pair<int, int>>q;
q.push({ r1,c1 });
visit[r1][c1] = 0;
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x == r2 && y == c2)
{
cout << visit[x][y] << endl;
return 0;
}
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
// 범위를 벗어남
if (check(xx, yy))
{
continue;
}
// 이동 도중 충돌
if ((xx == r2) && (yy == c2))
{
continue;
}
for (int k = 0; k < 2; k++)
{
int xxx = xx + dx2[i][k];
int yyy = yy + dy2[i][k];
// 범위를 벗어남
if (check(xxx, yyy))
{
continue;
}
// 이동 도중 충돌
if ((xxx == r2) && (yyy == c2))
{
continue;
}
xxx += dx2[i][k];
yyy += dy2[i][k];
// 범위를 벗어남
if (check(xxx, yyy))
{
continue;
}
if (visit[xxx][yyy] == -1)
{
visit[xxx][yyy] = visit[x][y] + 1;
q.push({ xxx,yyy });
}
}
}
}
cout << -1 << endl;
return 0;
}