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、右转。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
const int maxn=26;
int row,col,bex,bey,ans; //bex,bey是起点坐标
string maze[maxn]; //0:north,1:west,2:south,3:east
bool vis[maxn][maxn][4][5]; //green:0,black:1,red:2,blue:3,white:4
bool in(int x,int y){ return x>=0&&x<row&&y>=0&&y<col; } //是否在地图内
struct node // 保存4种状态以及花费
{
int x,y,dir,color;
int dist;
node(int x=0,int y=0,int dir=0,int color=0,int dist=0)
:x(x),y(y),dir(dir),color(color),dist(dist){}
};
queue<node> que;
void init()
{ while(!que.empty()) que.pop();
memset(vis,0,sizeof(vis));
}
void addnode(int x,int y,int dir,int color,int dist) // 增加节点
{ if(in(x,y)&&maze[x][y]!='#'&&!vis[x][y][dir][color]) // 在地图内,不是墙,且该种状态未被访问过
{
vis[x][y][dir][color]=true;
que.push(node(x,y,dir,color,dist));
}
}
void same_dir(int x,int y,int dir,int color,int dist) // 向前进
{
if(dir==0) addnode(x-1,y,dir,(color+1)%5,dist+1); //颜色要变化
else if(dir==1) addnode(x,y-1,dir,(color+1)%5,dist+1);
else if(dir==2) addnode(x+1,y,dir,(color+1)%5,dist+1);
else addnode(x,y+1,dir,(color+1)%5,dist+1);
}
void dir_change(int x,int y,int dir,int color,int dist)
{
for(int i=0;i<4;i++) //改变方向
{
if(i==dir||abs(i-dir)==2) continue;
addnode(x,y,i,color,dist+1);
}
}
bool solve()
{
init();
que.push(node(bex,bey,0,0,0)); //加入起点
vis[bex][bey][0][0]=true;
while(!que.empty())
{
node now=que.front(); que.pop();
if(maze[now.x][now.y]=='T'&&now.color==0){ ans=now.dist; return true; } //找到答案
int x=now.x, y=now.y, dir=now.dir, color=now.color, dist=now.dist;
same_dir(x,y,dir,color,dist);
dir_change(x,y,dir,color,dist);
}
return false;
}
int main()
{
int Case=0;
while(scanf("%d%d",&row,&col)!=EOF)
{ if(!row&&!col) break;
if(Case++) printf("\n");
for(int i=0;i<row;i++)
{
cin>>maze[i];
for(int j=0;j<col;j++)
if(maze[i][j]=='S') bex=i,bey=j;
}
printf("Case #%d\n",Case);
if(!solve()) printf("destination not reachable\n");
else printf("minimum time = %d sec\n",ans);
}
return 0;
}
UVA 10047-The Monocycle(队列bfs+保存4种状态)的更多相关文章
- 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 monocyle (四维bfs)
算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- CH 2601 - 电路维修 - [双端队列BFS]
题目链接:传送门 描述 Ha'nyu是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女Rika,从而被收留在地球上.Rika的家里有一辆飞行车.有一天飞行车的电路板突然出现了故障,导致 ...
随机推荐
- JS 实现 startWith endWith函数
String.prototype.startWith = function(s) { if (s == null || s == "" || this.length == 0 || ...
- 链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...
- [转载]用可变参数宏(variadic macros)传递可变参数表
注意:_VA_ARGS__ 从VS2005才开始支持 在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如: #define pr_debug(fmt,arg...) printk(KER ...
- Highcharts 非常实用的Javascript统计图
Highcharts 官网: http://www.highcharts.com Highcharts 官网示例:http://www.highcharts.com/demo/ Highcharts ...
- HDU 1827 Summer Holiday(Tarjan缩点)
Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity ...
- iPhone图形开发绘图小结
iPhone图形开发绘图教程是本文要介绍的内容,介绍了很多关于绘图类的使用,先来看详细内容讲解. 1.绘图总结: 绘图前设置: CGContextSetRGBFillColor/CGContextSe ...
- SOLR使用手册之操作collection
一.Collections API 参考:https://cwiki.apache.org/confluence/display/solr/Collections+API 因为API比较多,我就不一 ...
- RDLC添加链接
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdan ...
- Android 使用Facebook的 Stetho工具
Stetho在Android Studio中用: 1, 引入 compile 'com.facebook.stetho:stetho:1.3.1' compile 'com.facebook.stet ...
- iOS-OC-基础-NSDictionary常用方法
/*=============================NSDictionary(不可变字典)的使用=========================*/ //————————————————— ...