算法指南白书

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

 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)相关推荐

  1. 【Uva - 10047 】The Monocycle(搜索,bfs记录状态)

    题干: Uva的题目就不粘贴题干了,,直接上题意吧. 有你一个独轮车,车轮有5种颜色,为了5中颜色的相对位置是确定的.有两种操作:1.滚动:轮子必须沿着顺时针方向滚动,每滚动一次会到达另一个格子,着地 ...

  2. UVA 10047 - The Monocycle BFS

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. UVa 10047,独轮车

    题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...

  4. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  5. UVA 11165 - Galactic Travel(BFS+twopointer+并查集)

    UVA 11165 - Galactic Travel 题目链接 题意:给定一些不能走的边,要求出从s到t的最短路 思路:由于点数多,直接广搜会超时,所以加上优化,已经找过的点就不在重复找了,这点可以 ...

  6. 鬼吹灯之牧野诡事(四维bfs)

    鬼吹灯之牧野诡事 Problem Description 蓝色空间号和万有引力号进入了四维水洼,发现了四维物体–魔戒. 这里我们把飞船和魔戒都抽象为四维空间中的一个点,分别标为 "S&quo ...

  7. M - 魔戒(四维BFS)

    M - 魔戒 Description 蓝色空间号和万有引力号进入了四维水洼,发现了四维物体–魔戒. 这里我们把飞船和魔戒都抽象为四维空间中的一个点,分别标为 "S" 和 " ...

  8. UVA 1533 Moving Pegs(bfs+hash)

    题意: 一个跳棋,给定一个空位.求最少步数且字典序最小使得跳到最后棋盘只剩一个旗子,且在初始的空位上. 思路: BFS + hash判重,这题的思路还是比较好想的,但是我看网络上面的代码,都是打表+b ...

  9. 最短路算法详解(Dijkstra/SPFA/Floyd)

    转自:http://blog.csdn.net/murmured/article/details/19281031 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短 ...

最新文章

  1. mysql主从复制缺陷_mysql主从复制及遇到的坑
  2. ln -s 的一个坑
  3. Java讲课笔记18:异常处理
  4. 以上是对图像的椒盐噪声处理,在p_temp[j*wide+i]=0;这句程序中为什么要乘以wide,求解,谢谢!
  5. do_fork实现--上
  6. 【每日一具18】基于HTTP协议的局域网文件共享软件
  7. PC大作[微软模拟飞行10]
  8. python使用numpy按一定格式读取bin文件
  9. 基于深度学习的长江干线水位数据回归预测
  10. 计算机专业文献阅读报告,文献阅读报告范本.doc
  11. CNCC2020丨5G边缘智能与智慧城市论坛
  12. 纯干货:Linux抓包命令集锦
  13. 7-16 寻找大富翁
  14. 普罗米修斯 mysql监控_Promethus(普罗米修斯)监控Mysql数据库
  15. 如何将大量图片文件合并成一个*.bin文件
  16. 不要过分相信基础函数, 因为那也是人写的------警惕负负得正的现有逻辑之坑
  17. 为什么面试要问hashmap 的原理
  18. QGIS基本功| 6 图层进阶(三)- 快速美化图层数据
  19. BIOS和UEFI的区别,系统安装引导以及MBR和GPT磁盘分区
  20. 云开发山楂岛留言小程序带审核实现教程及源码

热门文章

  1. 单例模式反射、序列化漏洞及解决方案!
  2. 六种微服务架构的设计模式
  3. 拯救 Out Of Memory,8个案例带你飞!
  4. 微服务架构如何保障双11狂欢下的99.99%高可用
  5. 【手写系列】纯手写实现JDK动态代理
  6. JAVA: final 修饰符
  7. Java final关键字,常量的定义
  8. android studio编辑页面案例,2.4、Android Studio使用主题编辑器设计主题
  9. mysql 实时聚合分析_mysql滑动聚合/年初至今聚合原理与用法实例分析
  10. css 盒子有内容 盒子往下掉_css盒子模型与文本溢出学习笔记