UVa (BFS) The Monocycle

题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯。

所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方向朝dir与地面接触的扇形的颜色为color,这个状态是否到达过。

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 30;
 7 char maze[maxn][maxn];
 8 bool vis[maxn][maxn][4][5];
 9
10 struct Node
11 {
12     int x, y, d, t, step;
13     Node(int x=0, int y=0, int d=0, int t=0, int step=0):x(x), y(y), d(d), t(t), step(step) {}
14 }st, ed;
15
16 int dx[] = { -1, 0, 1, 0 };
17 int dy[] = { 0, 1, 0, -1 };
18
19 int row, col;
20
21 inline bool in(int x, int y)
22 { return x >= 0 && x < row && y >= 0 && y < col; }
23
24 int BFS()
25 {
26     memset(vis, false, sizeof(vis));
27     queue<Node> Q;
28     Q.push(st);
29     vis[st.x][st.y][0][0] = true;
30     while(!Q.empty())
31     {
32         Node now = Q.front(); Q.pop();
33         if(now.x == ed.x && now.y == ed.y && now.step % 5 == 0)
34             return now.t;
35
36         int d = now.d;
37         int x = now.x + dx[d];
38         int y = now.y + dy[d];
39         int t = now.t + 1;
40         int step = now.step + 1;
41         if(in(x, y) && maze[x][y] != ‘#‘ && !vis[x][y][d][step%5])
42         {
43             Q.push(Node(x, y, d, t, step));
44             vis[x][y][d][step%5] = true;
45         }
46
47         for(int i = 1; i <= 3; i += 2)
48         {
49             x = now.x; y = now.y;
50             d = (now.d + i) % 4;
51             t = now.t + 1;
52             step = now.step;
53             if(!vis[x][y][d][step%5])
54             {
55                 Q.push(Node(x, y, d, t, step));
56                 vis[x][y][d][step%5] = true;
57             }
58         }
59     }
60     return -1;
61 }
62
63 int main()
64 {
65     //freopen("in.txt", "r", stdin);
66
67     int kase = 0;
68     while(scanf("%d%d", &row, &col) == 2)
69     {
70         if(row == 0 && col == 0) break;
71
72         if(kase++ > 0) puts("");
73         printf("Case #%d\n", kase);
74
75         if(row == 0 && col == 0) break;
76         for(int i = 0; i < row; i++) scanf("%s", maze[i]);
77         for(int i = 0; i < row; i++)
78             for(int j = 0; j < col; j++)
79             {
80                 if(maze[i][j] == ‘S‘) st = Node(i, j, 0, 0, 0);
81                 if(maze[i][j] == ‘T‘) ed = Node(i, j);
82             }
83
84         int ans = BFS();
85         if(ans >= 0) printf("minimum time = %d sec\n", ans);
86         else puts("destination not reachable");
87     }
88
89     return 0;
90 }

代码君

时间: 2024-07-30 13:52:22

UVa (BFS) The Monocycle的相关文章

UVA 10047 - The Monocycle

题目如下:  Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five differentcolors as shown in the figure: The colored segments make equal an

uva 10047 - The Monocycle bfs

题目链接 A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as shown in the figure: The colored segments make equal angles (72o) at the center. A

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

UVA - 10047 The Monocycle (BFS)

题目大意:有一个n*m的网格,网格上面有的地方有障碍物 现在有一个人,骑着独轮车,要求从一个地方到达另一个地方,骑独轮车时,只能直走,或者左拐,右拐,不能向后走 独轮车的轮子被分成了5部分,每部分都有对应的颜色,刚开始时是绿色向下,当经过一个格子时,颜色就会变换 问从起点出发到终点,到终点时独轮车的绿色颜色向下,需要多久 解题思路:暴力BFS #include <cstdio> #include <cstring> #include <algorithm> #inclu

图论算法与模型(训练指南题库)

一.基础题目 1.UVA 11624 Fire!迷宫问题 多源BFS 题意: 帮助joe走出一个大火蔓延的迷宫,其中joe每分钟可往上下左右四个方向之一走,所有着火的格子都会蔓延(空格与着火格有公共边,下一分钟这个空格也会着火).迷宫中有一些障碍格,joe和火都无法进入,当joe走到一个边界的格子我们认为他走出了迷宫 输出R行C列的迷宫,#表示墙,.表示空地,J表示joe,F是着火格 如果能走出,输出最少时间否则,impossible 思路: 先预处理出每个点的起火时间,可以用多源点BFS直接处

uva 10047 uva live 2035 The Monocycle bfs

// uva 10047 uva live 2035 bfs // 求最短的嘛,肯定先尝试bfs啦 // 确定状态,首先状态里面得有坐标x,y // 还得有朝向,还得有颜色值 // // 这样就是一个状态里面有着三种属性 // 每个状态都只要经历一次,再经历是没有任何意义的 // 用一个que的思维数组记录就行了. // 按照方向爆搜,我先用f[i][j]记录的就是到 // 这一点的最小距离,但是怎么都过不了样例 // 突然明白了,如果只是这样记录最短距离,是不行的 // 因为每次从队列中取出的

UVA The Monocycle(BFS 4种状态)

 Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as shown in the figure: The colored segments make equal angles

UVA 10047-The Monocycle(队列bfs+保存4种状态)

题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间到达终点,如果无法到达,输出  destination not reachable,起点状态是朝北,着地颜色是绿色,到达终点的条件是着地颜色是绿色,方向任意. 解析:bfs搜一遍,但要保存4种状态,分别是坐标x,y,方向和颜色.每次选择有3种,1.向前进,2.左转,3.右转. 代码如下: #incl

uva 1318 - Monster Trap(bfs+暴力)

题目链接:uva 1318 - Monster Trap 每条线段2个点,加上起点终点一个是202个点,暴力判断连点之间是否可达,可达建边.因为线段有厚度考虑,所以将线段延伸一点再处理,这样原本共用一端点的线段变成相交.特殊情况是三点共线,这是判断延伸后的点是否落在其他线段上,如果是就不考虑这个点.最后做一遍bfs. #include <cstdio> #include <cstring> #include <cmath> #include <vector>