-
Notifications
You must be signed in to change notification settings - Fork 0
/
01 BFS.cpp
32 lines (32 loc) · 949 Bytes
/
01 BFS.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
char mat[N][N];
int dist[N][N], r, c;
bool valid(int x, int y){
if(x >= 1 && x <= r && y >= 1 && y <= c) return true;
return false;
}
int bfs01(int sx, int sy){
deque<pii>dq;
dq.push_front({sx,sy});
memset(dist, 63, sizeof(dist));
dist[sx][sy] = 0;
while(!dq.empty()){
pii top = dq.front();
dq.pop_front();
for(int i = 0; i < 4; i++){
int tx = top.ff+fx[i], ty = top.ss+fy[i];
if(valid(tx,ty)){
int x = dist[top.ff][top.ss];
if(mat[tx][ty] != mat[top.ff][top.ss]) x += 1;
if(x >= dist[tx][ty])continue;
dist[tx][ty] = x;
if(mat[tx][ty] == mat[top.ff][top.ss]){
dq.push_front({tx,ty});
}
else{
dq.push_back({tx,ty});
}
}
}
}
return dist[r][c];
}