UVa (BFS) The Monocycle
题目不光要求要到达终点而且要求所走的步数为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的更多相关文章
- UVA 10047 - The Monocycle BFS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 10047 The Monocycle
大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...
- 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 ...
- uva 10047 The Monocycle(搜索)
好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...
- UVA 10047-The Monocycle(队列bfs+保存4种状态)
题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间 ...
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- The Monocycle(BFS)
The Monocycle Time Limit: 3000MS64bit IO Format: %lld & %llu [Submit] [Go Back] [Status] Des ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
随机推荐
- datagridview 点击列标题排序
开发winform中,平时经常用到数据列表,我们大多选用datagridview,但是此控件本身没有排序的功能.参阅网上资料.留下标记,以后备用. datagridview的数据显示一般是通过数据绑定 ...
- hdu 3118(二进制枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
- 初学Ajax(二)
$.get()和$.post() .load()方法是局部方法,因为它需要一个包含元素的jQuery对象作为前缀.而$.get()和$.post()是全局方法,无须指定某个元素.对于用途而言,.loa ...
- [转]fedora启动telnet服务
http://blog.chinaunix.net/uid-22996709-id-3056078.html 在win7上安装了SecurityCRT,登录VMWARE Fedora时候登录超时,检查 ...
- lintcode:最长公共子序列
题目 最长公共子序列 给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度. 样例 给出"ABCD" 和 "EDCA",这个LCS是 "A& ...
- c# 事件为何要继承EventArgs
1:继承EventArgs是表示该类可作为事件,删掉了就默认继承object,没人会说你错 ----就是说事件不继承EventArgs 也没有错,也能正常运用,那么继承他的意义是什么呢?看2,3. 觉 ...
- HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)
Seam Carving DescriptionFish likes to take photo with his friends. Several days ago, he found that s ...
- Linux实施一次性任务
一.定义 at命令:定时任务,指定一个时间执行一个任务,只能执行一次. 二.选项 三.示例 1. 使用 student 用户,创建一个任务,2 分钟以后执行,在/home/student/at.txt ...
- chrome开发配置(四)生成项目及配置库引用
1.运行gclient runhooks --force .会重新下载个python 2.7版本的,并且下载好几个文件,大概1个多小时:
- SQL SERVER ->> Columnstore Index
谈到Columnstore index就不得不提SQL SERVER的压缩技术了.Columnstore就是用到了SQL SERVER的压缩技术.Columnstore又分Columnstore和Co ...