日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uva 10047 the monocyle (四维bfs)

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uva 10047 the monocyle (四维bfs) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法指南白書

維護一個四維數組,走一步更新一步

1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 7 const int INF = 1000000000; 8 const int maxr = 25 + 5; 9 const int maxc = 25 + 5; 10 int R, C, sr, sc, tr, tc; 11 char maze[maxr][maxc]; 12 13 struct State { 14 int r, c, dir, color; 15 State(int r, int c, int dir, int color):r(r),c(c),dir(dir),color(color) {} 16 }; 17 18 const int dr[] = {-1,0,1,0}; // north, west, south, east 19 const int dc[] = {0,-1,0,1}; 20 int d[maxr][maxc][4][5], vis[maxr][maxc][4][5];//橫坐標,縱坐標,方向,顏色 21 22 int ans; 23 queue<State> Q; 24 25 void update(int r, int c, int dir, int color, int v) { 26 if(r < 0 || r >= R || c < 0 || c >= C) return; // 不能走出邊界 27 if(maze[r][c] == '.' && !vis[r][c][dir][color]) { 28 Q.push(State(r, c, dir, color)); 29 vis[r][c][dir][color] = 1; 30 d[r][c][dir][color] = v; 31 if(r == tr && c == tc && color == 0) ans = min(ans, v); // 更新答案 32 } 33 } 34 35 void bfs(State st) { 36 d[st.r][st.c][st.dir][st.color] = 0; 37 vis[st.r][st.c][st.dir][st.color] = 1; 38 Q.push(st); 39 while(!Q.empty()) { 40 st = Q.front(); 41 Q.pop(); 42 int v = d[st.r][st.c][st.dir][st.color] + 1; 43 update(st.r, st.c, (st.dir+1)%4, st.color, v); // 左轉 44 update(st.r, st.c, (st.dir+3)%4, st.color, v); // 右轉 45 update(st.r+dr[st.dir], st.c+dc[st.dir], st.dir, (st.color+1)%5, v); // 前進 46 } 47 } 48 49 int main() { 50 int kase = 0; 51 while(scanf("%d%d", &R, &C) == 2 && R && C) { 52 for(int i = 0; i < R; i++) { 53 scanf("%s", maze[i]); 54 for(int j = 0; j < C; j++) 55 if(maze[i][j] == 'S') { 56 sr = i; 57 sc = j; 58 } else if(maze[i][j] == 'T') { 59 tr = i; 60 tc = j; 61 } 62 } 63 maze[sr][sc] = maze[tr][tc] = '.'; 64 ans = INF; 65 memset(vis, 0, sizeof(vis)); 66 bfs(State(sr, sc, 0, 0)); 67 68 if(kase > 0) printf("\n"); 69 printf("Case #%d\n", ++kase); 70 if(ans == INF) printf("destination not reachable\n"); 71 else printf("minimum time = %d sec\n", ans); 72 } 73 } View Code

?

轉載于:https://www.cnblogs.com/ITUPC/p/5312976.html

總結

以上是生活随笔為你收集整理的uva 10047 the monocyle (四维bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。