Skip to content

Commit 2394c23

Browse files
authored
Merge pull request #1638 from AlgorithmWithGod/khj20006
[20251210] BOJ / G5 / 공주님을 구해라! / 권혁준
2 parents 788ae00 + 6dc3dae commit 2394c23

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
const int dx[4] = {1,0,-1,0};
6+
const int dy[4] = {0,1,0,-1};
7+
8+
int N, M, T;
9+
int arr[100][100]{};
10+
queue<tuple<int, int, int>> q;
11+
bool vis[100][100]{};
12+
int paint = 1234567;
13+
14+
int main() {
15+
cin.tie(0)->sync_with_stdio(0);
16+
17+
cin>>N>>M>>T;
18+
for(int i=0;i<N;i++) for(int j=0;j<M;j++) cin>>arr[i][j];
19+
20+
q.emplace(0,0,0);
21+
vis[0][0] = 1;
22+
while(!q.empty()) {
23+
auto [x,y,t] = q.front(); q.pop();
24+
if(arr[x][y] == 2) {
25+
paint = t + abs(N-x-1) + abs(M-y-1);
26+
}
27+
if(x == N-1 && y == M-1) {
28+
int val = min(paint, t);
29+
if(val > T) cout<<"Fail";
30+
else cout<<val;
31+
return 0;
32+
}
33+
for(int i=0;i<4;i++) {
34+
int xx = x+dx[i], yy = y+dy[i];
35+
if(xx<0 || xx>=N || yy<0 || yy>=M || vis[xx][yy] || arr[xx][yy] == 1) continue;
36+
vis[xx][yy] = 1;
37+
q.emplace(xx,yy,t+1);
38+
}
39+
}
40+
if(paint > T) cout<<"Fail";
41+
else cout<<paint;
42+
43+
}
44+
```

0 commit comments

Comments
 (0)