题意:给你一张地图,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种状态)的更多相关文章

  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 monocyle (四维bfs)

    算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...

  6. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  7. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  8. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  9. CH 2601 - 电路维修 - [双端队列BFS]

    题目链接:传送门 描述 Ha'nyu是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女Rika,从而被收留在地球上.Rika的家里有一辆飞行车.有一天飞行车的电路板突然出现了故障,导致 ...

随机推荐

  1. JS的substr与substring的区别

    substr返回从指定位置开始的指定长度的子字符串 str.substr(star[,length])  第二个参数可选,不选的话,截取到最后,如果length为0或者负数,那么返回的将是一个空字符串 ...

  2. Only2 Labs — A Visual Design Studio

    Only2 Labs - A Visual Design Studio 设计合作 对您目前的设计很不满意?或是急缺一个设计供应商?您的团队最近做的项目需要指导?Only2都很乐意为您解困惑. 或者,你 ...

  3. DreamWeaver文件保存时,提示"发生共享违例"问题的解决方法

    在学习牛腩老师的JS视频中,视频中的例子要求实现一个是23个3相乘的结果,在用Dreamweaver制作时,, <script language="javascript" t ...

  4. Linux下使用JNI的常见问题及解决方案

    JNI是java和C/C++混合编程的接口,可以很方便地实现java调用C/C++语言.具体的使用方法,网上有很多教程,在此不做过多介绍.本博客只关注在使用JNI的过程中的常见问题. 1.     生 ...

  5. C++ Primer笔记1_转义字符_标准库类型string_标准库类型vector

    1.转义字符 一般有两种方式: \x后紧跟1个或多个十六进制数字.或\后紧跟1.2.3个八进制数字,当中数字部分是字符相应的数值. #include <iostream> using na ...

  6. ArrayList的分析(转)

    一. ArrayList概述: ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境 ...

  7. CPU性能测试

    用计算圆周率的办法来测试cpu性能 4*a(1) 是 bc 主動提供的一個計算 pi 的函數,至於 scale 就是要 bc 計算幾個小數點下位數的意思.當 scale 的數值越大, 代表 pi 要被 ...

  8. IoC容器Autofac之IOC/DI基本概念(二)

    原文:http://www.cnblogs.com/xdp-gacl/p/4249939.html 1.1.IoC是什么 Ioc—Inversion of Control,即“控制反转”,一种设计思想 ...

  9. js keycode 列表

    keycode    8 = BackSpace BackSpace keycode    9 = Tab Tab keycode   12 = Clear keycode   13 = Enter ...

  10. 玩转Nodejs日志管理log4js(转)

    转自:http://blog.fens.me/nodejs-log4js/ 前言 日志对任何的应用来说都是至关重要的.在Nodejs中使用express框架并没有自带的日志模块,我们可以选择log4j ...