[BOJ] 17244 아맞다우산

Time Lapse :19min 34sec

17244.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
#include <stdio.h>
#include <queue>
using namespace std;
struct INFO{
int y;
int x;
int item;
int mv;
};
int N, M;
int v[50][50][32];
char buf[50][51];
int Pos[2], num, ans = 987654321;
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
queue<INFO> q;
int main(){
scanf("%d %d",&M,&N);
for(int i = 0; i < N; ++i){
scanf("%s",buf[i]);
for(int j = 0; j < M; ++j){
if(buf[i][j]=='S') Pos[0] = i, Pos[1] = j;
else if(buf[i][j]=='X') buf[i][j] = (num++|0b110000);
}
}
q.push({Pos[0],Pos[1],0,1});
v[Pos[0]][Pos[1]][0] = 1;
num = (1<<num)-1;
while(!q.empty()){
int x = q.front().x;
int y = q.front().y;
int item = q.front().item;
int mv = q.front().mv;
q.pop();
if(v[y][x][item]!=mv) continue;
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(0<=nx&&nx<M && 0<=ny&&ny<N && buf[ny][nx]!='#'&& (!v[ny][nx][item]||v[ny][nx][item]>mv+1)){
v[ny][nx][item] = mv+1;
if(buf[ny][nx]=='E'){
if(item==num)
ans = ans > mv ? mv : ans;
}
else if('0'<=buf[ny][nx] && buf[ny][nx]<='5')
v[ny][nx][(item|(1<<(buf[ny][nx]&0b1111)))] = mv+1, q.push({ny,nx,(item|(1<<(buf[ny][nx]&0b1111))),mv+1});
else q.push({ny,nx,item,mv+1});
}
}
}
printf("%d",ans);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/17244/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.