这题思路就普通的BFS加上一个维度朝向,主要是要注意输入,输出,以及细节的处理

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std; const int MAX = 9+1;
const int DIR = 4;
const int TURN = 3;
bool have_edge[MAX][MAX][DIR][TURN];//所在位置 , 朝向 , 要走的方向 int d[MAX][MAX][DIR];//最短路径 同时可以判断结点是否访问过
#define Node(x,n) x[n.r][n.c][n.dir] struct node
{
int r , c, dir;
node(){}
node(int r, int c, int dir):r(r),c(c),dir(dir){}
}; struct node p[MAX][MAX][DIR];//父节点 打印 路径
const char *dirs = "NESW";//clockwise
const char *turns = "LFR"; inline int dir_id(char c) {return strchr(dirs,c)-dirs;}
inline int turn_id(char c) {return strchr(turns,c)-turns;} const int MAX_NAME_LEN = 42;
char MazeName[MAX_NAME_LEN];//20个字符,还有空格。。。
int r0,c0;
int r1,c1,dir1;
int r2,c2;
int dr[] = {-1, 0, 1, 0};//clockwise
int dc[] = { 0, 1, 0,-1}; bool read()
{
gets(MazeName);//lineread
// if(!strcmp(MazeName,"END")) return false;
char s[10]; int r, c;
if(!~scanf("%d%d%s",&r0,&c0,s)) return false;
printf("%s\n",MazeName);
memset(have_edge,false,sizeof(have_edge)); dir1 = dir_id(s[0]);
r1 = r0 + dr[dir1]; c1 = c0 + dc[dir1];
scanf("%d%d",&r2,&c2); while(scanf("%d",&r),r) {
scanf("%d",&c);
while(scanf("%s",s),*s != '*'){
char *p = s; int dir = dir_id(*s);//!!
while(*(++p)) { have_edge[r][c][dir][turn_id(*p)] = true;}
}
}
getchar();//吸收换行符
return true;
} void print_ans(node u)
{
vector<node> ans;
for(;;){
ans.push_back(u);
if(Node(d,u) == 0) break;
u = Node(p,u);
}
ans.push_back(node(r0,c0,dir1));
int cnt = 0;
for(int i = ans.size() - 1; i >= 0; i--){
if(cnt%10 == 0) printf(" ");
printf(" (%d,%d)", ans[i].r, ans[i].c);
if(++cnt%10 == 0) puts("");
}
if(ans.size()%10) puts("");//注意
} node walk(node &u,int turn){
int dir = u.dir;
if(turn == 0) dir = (dir + 3)%4;//rev
else if(turn == 2) dir = (dir + 1)%4;//clockwise
return node(u.r + dr[dir], u.c + dc[dir], dir );
}
inline bool inside(int r,int c) { return r > 0&&r <= 9 && c > 0 && c <= 9; } void solve()
{
queue<node> q;
node u(r1,c1,dir1);//c1 - >c2
memset(d, -1, sizeof(d));
d[u.r][u.c][u.dir] = 0;
q.push(u);
while(!q.empty()){
u = q.front();q.pop();
if(u.r == r2 && u.c == c2) { print_ans(u); return ;}
for(int i = 0; i < 3; i++) {
if(!have_edge[u.r][u.c][u.dir][i]) continue;
node v = walk(u,i);
if(inside(v.r, v.c) && d[v.r][v.c][v.dir] < 0){
d[v.r][v.c][v.dir] = Node(d,u) + 1;
Node(p,v) = u;
q.push(v);//push(u) ...
}
}
}
printf(" No Solution Possible\n");
} int main()
{
#ifdef local
freopen("in2.txt","r",stdin);
// freopen("myout.txt","w",stdout);
#endif // local
while(read()){
solve();
}
return 0;
}

  

[uva816]AbbottsRevenge Abbott的复仇(经典迷宫BFS)的更多相关文章

  1. Uva 816 Abbott的复仇(三元组BFS + 路径还原)

    题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...

  2. 【OpenJ_Bailian - 2790】迷宫(bfs)

    -->迷宫  Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...

  3. 一道很经典的 BFS 题

    一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...

  4. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  5. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  6. 6_14 Abbott的复仇(UVa816)<图的最短路BFS>

    1999次世界总决赛的比赛包括一个骰子迷宫问题.在这个问题被写的时候,法官们无法发现骰子迷宫概念的原始来源.不久之后的比赛,但是,罗伯特先生雅培,无数的迷宫和对作者的创造者主题,联系大赛评委,自称是骰 ...

  7. 算法入门经典第六章 例题6-14 Abbott的复仇(Abbott's Revenge)BFS算法实现

    Sample Input 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ELF * 2 3 SF ...

  8. UVa 816 Abbott的复仇(BFS)

    寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...

  9. UVA816 Abbott's Revenge (三元组BFS)

    题目描述: 输入输出: 输入样例: SAMPLE 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ...

随机推荐

  1. SpringMVC使用fastjson自定义Converter支持返回jsonp格式(转)

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import c ...

  2. C# 中的迭代器 yield关键字 提高性能和可读性

    展示一个例子 IList<string> FindBobs(IEnumerable<string> names) { var bobs = new List<string ...

  3. 蜂窝网络TDOA定位方法的Fang算法研究及仿真纠错

    科学论文为我们提供科学方法,在解决实际问题中,能极大提高生产效率.但论文中一些失误则可能让使用者浪费大量时间.自己全部再推导那真不容易,怀疑的成本特别高,通常不会选择这条路.而如果真是它的问题,其它所 ...

  4. MySQL8.0.16新特性:The Communication Protocol In Group Replication

    MGR优雅升级到MySQL8.0.16 传统的升级手段之一,5.7 MGR集群与8.0 MGR集群进行数据传输,程序切换新集群后测试是否正常. 如果不正常,要么将新集群的新增数据同步回旧集群,要么就舍 ...

  5. org.apache.commons.httpclient.HttpClient的使用(转)

    HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提供了访 ...

  6. Unity3d导入3dmax后model 的缩放为0.0254的原因以及解决办法

    http://blog.csdn.net/pdw_jsp/article/details/51259493 这个问题其实已经早都出现过了,今天我们这边也碰到了,这里做个记录吧 导致的问题~ 场景的比例 ...

  7. python爬虫——web前端基础(1)

    1.HTML的基本结构 <html>内容</html>:HTML文档是由<html></html>包裹,这是HTML文档的文档标记,也称为HTML开始标 ...

  8. php:获取一个表不含text类型的全部字段

    select * from table 这个*用表具体的字段替换 $sql="show COLUMNS FROM table"; $rs=query($sql); while($r ...

  9. (转)AIX 中 Paging Space 使用率过高的分析与解决

    AIX 中 Paging Space 使用率过高的分析与解决 原文:https://www.ibm.com/developerworks/cn/aix/library/au-cn-pagingspac ...

  10. (转)Module ngx_http_fastcgi_module

    Example ConfigurationDirectives     fastcgi_bind     fastcgi_buffer_size     fastcgi_buffering     f ...