-
Notifications
You must be signed in to change notification settings - Fork 2
/
17836_josueyeon.cpp
149 lines (122 loc) · 3.07 KB
/
17836_josueyeon.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// BFS: 두번 BFS 진행해주면 됨
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int graph[101][101];
int visited[101][101];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int BFS(int n, int m)
{
queue<pair<pair<int, int>, int>> q;
q.push({{0, 0}, 0});
while(!q.empty())
{
int x = q.front().first.first;
int y = q.front().first.second;
int time = q.front().second;
q.pop();
for(int dir = 0;dir < 4;dir++)
{
int tempX = x + dx[dir];
int tempY = y + dy[dir];
if (tempX < 0 || tempX >= n || tempY < 0 || tempY >= m)
continue;
if (graph[tempX][tempY] != 1 && visited[tempX][tempY] == 0)
{
visited[tempX][tempY] = time + 1;
q.push({{tempX, tempY}, time + 1});
}
}
}
return visited[n - 1][m - 1];
}
int swordBFS(int n, int m, int swordX, int swordY)
{
if (visited[swordX][swordY] == 0)
return 0;
int temp;
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
if (i == swordX && j == swordY)
temp = visited[i][j];
visited[i][j] = 0;
}
}
queue<pair<pair<int, int>, int>> q;
q.push({{swordX, swordY}, temp});
visited[swordX][swordY] = temp;
while(!q.empty())
{
int x = q.front().first.first;
int y = q.front().first.second;
int time = q.front().second;
q.pop();
for(int dir = 0;dir < 4;dir++)
{
int tempX = x + dx[dir];
int tempY = y + dy[dir];
if (tempX < 0 || tempX >= n || tempY < 0 || tempY >= m)
continue;
if (visited[tempX][tempY] == 0)
{
visited[tempX][tempY] = time + 1;
q.push({{tempX, tempY}, time + 1});
}
}
}
return visited[n - 1][m - 1];
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
// N(집 크기)
int n, m, T;
cin>>n>>m>>T;
int swordx, swordy;
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
cin>>graph[i][j];
if (graph[i][j] == 2)
{
swordx = i;
swordy = j;
}
}
}
int ans1 = BFS(n, m);
int ans2 = swordBFS(n, m, swordx, swordy);
int ans = min(ans1, ans2);
if (ans > T ||(ans1 == 0 && ans2 == 0))
{
cout<<"Fail\n";
return 0;
}
else if (ans1 * ans2 == 0)
{
if (ans1 == 0)
{
if (ans2 > T)
cout<<"Fail\n";
else
cout<<ans2<<"\n";
}
else
{
if (ans1 > T)
cout<<"Fail\n";
else
cout<<ans1<<"\n";
}
}
else
cout<<ans<<"\n";
return 0;
}