题意:给你一张地图,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. Sql Server 2005的1433端口打开和进行远程连接

    参考地址:http://topic.csdn.net/u/20090828/16/e693935a-99b7-4090-a6bc-0123c91183eb.html 1.如何打开sql server  ...

  2. MongoDB安装,打开及增,删,改,查

    MongoDB是目前最流行的NoSQl之一,NoSQL及No Only Sql,之所以这样叫我的理解是它摒弃了传统关系型数据库的字段类型的概念,而是将所有的数据以key-value方式存储,以key索 ...

  3. 应用程序打包(ipa)

    如果想让用户可以安装ipa, 必须在打包程序的时候说清楚哪一个应用程序(appid)可以安装到哪一台设备上.(UDID). 原理: 要想打包, 告诉苹果, 哪一台电脑可以进行打包 步骤: 让电脑端具备 ...

  4. onethink 验证码二维码不显示的问题

    常规思路: 1 检查GD和FreeType.在项目根目录(index.php同级)下放一个php文件 <?php echo phpinfo(); ?> 访问此文件,查看GD和FreeTyp ...

  5. 04747_Java语言程序设计(一)_第9章_输入和输出流

    例9.1一个文件复制应用程序,将某个文件的内容全部复制到另一个文件. import java.io.*; public class Example9_1 { public static void ma ...

  6. SQL生成随机字符串

    1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')

  7. 将string 转int

    /** * @param {string} str * @return {number} */ var myAtoi = function(str) { str = str.replace(/^(\s ...

  8. FileSystemWatcher使用方法具体解释

    FileSystemWatcher控件主要功能: 监控指定文件或文件夹的文件的创建.删除.修改.重命名等活动.能够动态地定义须要监控的文件类型及文件属性修改的类型. 1.经常使用的几个基本属性: (1 ...

  9. js转换ascii编码如中文友转换为编码友;可防止乱码

  10. Dreamwaver 使用root用户连接不上远程服务器

    我用dreamweaver连接远程服务,开始用的是root用户登录的,但是连接不上.网上查了一下,解决教程非常复杂,我就不列出来了. 后来我想了一下,之前我有连接过.我感觉可能是用户的问题,于是我在远 ...