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

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

 #include <cstdio>
#include <queue>
#include <cstring>
using namespace std; const int maxn = ;
char maze[maxn][maxn];
bool vis[maxn][maxn][][]; struct Node
{
int x, y, d, t, step;
Node(int x=, int y=, int d=, int t=, int step=):x(x), y(y), d(d), t(t), step(step) {}
}st, ed; int dx[] = { -, , , };
int dy[] = { , , , - }; int row, col; inline bool in(int x, int y)
{ return x >= && x < row && y >= && y < col; } int BFS()
{
memset(vis, false, sizeof(vis));
queue<Node> Q;
Q.push(st);
vis[st.x][st.y][][] = true;
while(!Q.empty())
{
Node now = Q.front(); Q.pop();
if(now.x == ed.x && now.y == ed.y && now.step % == )
return now.t; int d = now.d;
int x = now.x + dx[d];
int y = now.y + dy[d];
int t = now.t + ;
int step = now.step + ;
if(in(x, y) && maze[x][y] != '#' && !vis[x][y][d][step%])
{
Q.push(Node(x, y, d, t, step));
vis[x][y][d][step%] = true;
} for(int i = ; i <= ; i += )
{
x = now.x; y = now.y;
d = (now.d + i) % ;
t = now.t + ;
step = now.step;
if(!vis[x][y][d][step%])
{
Q.push(Node(x, y, d, t, step));
vis[x][y][d][step%] = true;
}
}
}
return -;
} int main()
{
//freopen("in.txt", "r", stdin); int kase = ;
while(scanf("%d%d", &row, &col) == )
{
if(row == && col == ) break; if(kase++ > ) puts("");
printf("Case #%d\n", kase); if(row == && col == ) break;
for(int i = ; i < row; i++) scanf("%s", maze[i]);
for(int i = ; i < row; i++)
for(int j = ; j < col; j++)
{
if(maze[i][j] == 'S') st = Node(i, j, , , );
if(maze[i][j] == 'T') ed = Node(i, j);
} int ans = BFS();
if(ans >= ) printf("minimum time = %d sec\n", ans);
else puts("destination not reachable");
} return ;
}

代码君

UVa (BFS) The Monocycle的更多相关文章

  1. UVA 10047 - The Monocycle BFS

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

  2. UVA 10047 The Monocycle

    大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...

  3. UVA 10047 The Monocycle (状态记录广搜)

    Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be consi ...

  4. uva 10047 The Monocycle(搜索)

    好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...

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

    题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间 ...

  6. UVa 439骑士的移动(BFS)

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

  7. The Monocycle(BFS)

    The Monocycle Time Limit: 3000MS64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Des ...

  8. UVa 11624,两次BFS

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

  9. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

随机推荐

  1. javascript陷阱,一不小心你就中招了

  2. Jmeter测试Mysql

    一.在测试计划下,找到Add directory or jar to classpath下填入jdbc驱动路径. 二.新建线程组. 三.在线程组下,添加配置元件—JDBC Connection Con ...

  3. POJ 3662

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4591   Accepted: 1693 D ...

  4. swift函数和初始化控件(// MARK:分割线)

    import UIKit , , , )         view.backgroundColor = UIColor.redColor()         self.view.addSubview( ...

  5. POJ 2127

    #include <iostream> #define MAXN 501 using namespace std; int a[MAXN],b[MAXN],ans[MAXN]; int G ...

  6. EF框架 完整项目记录

    今天终于能用EF框架搭建一个项目,同时能连接sqlserver 数据库.mysql 数据库 1.必须的文件 2.将必须文件导入项目,此处用到“管理 NuGet 程序包”,由于从网上下载比较慢,此处从本 ...

  7. ***php(codeigniter)中如何重定向

    Q: 在保存完数据之后需要重定向,防止数据重复提交. 我使用$this->方法名();跳转,发现不能达到重定向的效果(地址栏没变) 请教高手重定向怎么用 A: $this->load-&g ...

  8. Android Grapics图像类体系

  9. JDK环境变量解析

    设置环境变量 在java 中需要设置三个环境变量(1.5之后不用再设置classpath了,但个人强烈建议继续设置以保证向下兼用问题)JDK安装完成之后我们来设置环境变量:右击“我的电脑”,选择“属性 ...

  10. C#计算时间差值

    /// <summary> /// 计算时间差值 /// </summary> /// <param name="DateTime1">< ...