uva 10047 the monocyle (四维bfs)

时间:2022-12-09 18:31:20

算法指南白书

维护一个四维数组,走一步更新一步

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int INF = ;
const int maxr = + ;
const int maxc = + ;
int R, C, sr, sc, tr, tc;
char maze[maxr][maxc]; struct State {
int r, c, dir, color;
State(int r, int c, int dir, int color):r(r),c(c),dir(dir),color(color) {}
}; const int dr[] = {-,,,}; // north, west, south, east
const int dc[] = {,-,,};
int d[maxr][maxc][][], vis[maxr][maxc][][];//横坐标,纵坐标,方向,颜色 int ans;
queue<State> Q; void update(int r, int c, int dir, int color, int v) {
if(r < || r >= R || c < || c >= C) return; // 不能走出边界
if(maze[r][c] == '.' && !vis[r][c][dir][color]) {
Q.push(State(r, c, dir, color));
vis[r][c][dir][color] = ;
d[r][c][dir][color] = v;
if(r == tr && c == tc && color == ) ans = min(ans, v); // 更新答案
}
} void bfs(State st) {
d[st.r][st.c][st.dir][st.color] = ;
vis[st.r][st.c][st.dir][st.color] = ;
Q.push(st);
while(!Q.empty()) {
st = Q.front();
Q.pop();
int v = d[st.r][st.c][st.dir][st.color] + ;
update(st.r, st.c, (st.dir+)%, st.color, v); // 左转
update(st.r, st.c, (st.dir+)%, st.color, v); // 右转
update(st.r+dr[st.dir], st.c+dc[st.dir], st.dir, (st.color+)%, v); // 前进
}
} int main() {
int kase = ;
while(scanf("%d%d", &R, &C) == && R && C) {
for(int i = ; i < R; i++) {
scanf("%s", maze[i]);
for(int j = ; j < C; j++)
if(maze[i][j] == 'S') {
sr = i;
sc = j;
} else if(maze[i][j] == 'T') {
tr = i;
tc = j;
}
}
maze[sr][sc] = maze[tr][tc] = '.';
ans = INF;
memset(vis, , sizeof(vis));
bfs(State(sr, sc, , )); if(kase > ) printf("\n");
printf("Case #%d\n", ++kase);
if(ans == INF) printf("destination not reachable\n");
else printf("minimum time = %d sec\n", ans);
}
}