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方法都大有裨益, 敬请有兴趣者 ...
随机推荐
- 3[doses] ------一种诡异的写法
在 head first c 的第60页,有这么一道题: 一个富翁因为服药过度而死亡. 下面是自动服药器的代码: #include <stdio.h> int main(void) { , ...
- DB2 DATE类型在显示的时候,带有00:00:00,去掉的方法,使用VARCHAR()函数
DROP VIEW DMS.V_AQ_INSURANCECLAIMS; CREATE VIEW DMS.V_AQ_INSURANCECLAIMS AS SELECT * FROM (SELECT T1 ...
- Useful for Android the development engineer from Github
Original:http://sysmagazine.com/posts/216591/ Many plowing on open space Github, I found assemblage ...
- 网络处理2-异步POST请求和同步请求
一.异步POST请求 假如请求路径是http://192.168.1.102:8080/MJServer/login,请求参数有2个: username :母鸡 pwd :123 1.POST请求细节 ...
- lintcode:验证二叉查找树
题目 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个 ...
- IOS中延时执行的几种方式的比较
本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...
- Java Servlet 技术简介
Java Servlet 技术简介 Java 开发人员兼培训师 Roy Miller 将我们现有的 servlet 介绍资料修改成了这篇易于学习的实用教程.Roy 将介绍并解释 servlet 是什么 ...
- DB2_SQL_常用知识点&实践
DB2_SQL_常用知识点&实践 一.删除表中的数据(delete或truncate) 1 truncate table T_USER immediate; 说明:Truncate是一个能够快 ...
- 280. Wiggle Sort
题目: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] ...
- Java:装饰设计模式
装饰设计模式: 当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能, 并提供加强功能,那么自定义的该类就称为装饰类. 装饰类通常通过构造方法接收被装饰的对象,并基于被装饰的对 ...