本题大意:给定一个迷宫,让你判断是否能从给定的起点到达给定的终点,这里起点需要输入起始方向,迷宫的每个顶点也都有行走限制,每个顶点都有特殊的转向约束...具体看题目便知...

  本题思路:保存起点和终点的状态,保存每个顶点的状态,包括每个方向的可转向方向,然后直接BFS即可,记得保存每个儿子结点的爹,便于回溯输出路径。

  参考代码:

 #include <bits/stdc++.h>
using namespace std; struct Node{
int r, c, dir;// 表示结点的横纵坐标和方向
Node(int r = , int c = , int dir = ) :r(r), c(c), dir(dir) {};//对结构体元素进行初始化
};
const int maxn = ;
const char *dirs = "NESW";
const char *turns = "FLR";
const int dr[] = {-, , , };
const int dc[] = {, , , -};
int has_edge[maxn][maxn][][];//保存(r, c, dir)状态下的可移动方向
int d[maxn][maxn][];//存储初始状态到(r, c, dir)的最短路长度
Node p[maxn][maxn][];//同时用p[r][c][dir]保存了状态(r, c, dir)在BFS树中的父节点
int r0, c0, dir, r1, c1, r2, c2;
char name[]; int dir_id(char c) {
return strchr(dirs, c) - dirs;
} int turn_id(char c) {
return strchr(turns, c) - turns;
} Node walk(const Node &u, int turn) {
int dir = u.dir;
if(turn == ) dir = (dir + ) % ;//逆时针
if(turn == ) dir = (dir + ) % ;//顺时针
return Node(u.r + dr[dir], u.c + dc[dir], dir);
} bool inside(int r, int c) {
return r >= && r <= && c >= && c <=;
} bool read_case () {
char s[], s2[];
scanf("%s", name);
if(!strcmp(name, "END")) return false;
scanf("%d %d %s %d %d", &r0, &c0, s2, &r2, &c2);
cout << name << endl;
dir = dir_id(s2[]);
r1 = r0 + dr[dir];
c1 = c0 + dc[dir];
memset(has_edge, , sizeof(has_edge));
while(true) {
int r, c;
scanf("%d", &r);
if(r == ) break;
scanf("%d", &c);
while(~scanf("%s", s) && s[] != '*') {
for(int i = ; i < strlen(s); i ++)
has_edge[r][c][dir_id(s[])][turn_id(s[i])] = ;
}
}
return true;
} void print_ans(Node u) {
//从目标节点逆序回溯到初始结点
vector <Node> nodes;
while(true) {
nodes.push_back(u);
if(d[u.r][u.c][u.dir] == ) break;
u = p[u.r][u.c][u.dir];
}
nodes.push_back(Node(r0, c0, dir));
//打印解,每行10个
int cnt = ;
for(int i = nodes.size() - ; i >= ; i --) {
if(cnt % == ) printf(" ");
printf(" (%d,%d)", nodes[i].r, nodes[i].c);
if(++ cnt % == ) printf("\n");
}
if(nodes.size() % != ) printf("\n");
} void solve () {
queue <Node> q;
memset(d, -, sizeof(d));
Node u(r1, c1, dir);
d[u.r][u.c][u.dir] = ;
q.push(u);
while(!q.empty()) {
Node u = q.front();
q.pop();
if(u.r == r2 && u.c == c2) {
print_ans(u);
return;
}
for(int i = ; i < ; i ++) {
Node v = walk(u, i);
if(has_edge[u.r][u.c][u.dir][i] && inside(v.r, v.c) && d[v.r][v.c][v.dir] < ) {
d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + ;
p[v.r][v.c][v.dir] = u;
q.push(v);
}
}
}
printf(" No Solution Possible\n");
} int main () {
while(read_case()) {
solve();
}
return ;
}

UVA-816.Abbott's Tevenge (BFS + 打印路径)的更多相关文章

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

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

  2. Uva 816 Abbott's Revenge(BFS)

    #include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...

  3. UVA 816 - Abbott&#39;s Revenge(BFS)

    UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...

  4. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  5. UVa 103 - Stacking Boxes (LIS,打印路径)

    链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...

  6. Uva 10131 Is Bigger Smarter? (LIS,打印路径)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...

  7. Codeforces 3A-Shortest path of the king(BFS打印路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  8. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

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

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

随机推荐

  1. eclipse gradle插件 org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradle-3.4-bin.zip'.

    eclipse安装gradle后出现如下异常: org.gradle.tooling.GradleConnectionException: Could not install Gradle distr ...

  2. Mac安装Python3报错Permission denied @ dir_s_mkdir - /usr/local/Frameworks

    brew安装Python3时出现的问题: Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks /usr/local/Frame ...

  3. python_04 基本数据类型、数字、字符串、列表、元组、字典

    基本数据类型 所有的方法(函数)都带括号,且括号内没带等号的参数需传给它一个值,带等号的参数相当于有默认值 1.数字 int 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1 ...

  4. hive,分桶,内外部表,分区

    简单的word-count操作: [root@master test-map]# head -10 The_Man_of_Property.txt    #先看看数据Preface“The Forsy ...

  5. python 阿狸的进阶之路(5)

    一.模块 1.什么是模块: 包含了一组功能的python文件,文件名是xxx.py,模块名是module. 可以使用 import module,四个通用的类别: (1)用python编写的py文件 ...

  6. Django--ORM(模型层)--多表(重重点)

    一.数据库表关系 单表 重复的字段太多,所以需要一对多,多对多来简化 多表 多对一 多对多 一对一 =============================================== 一对 ...

  7. cordova-config.xml配置应用图标

    1. <icon src="res/icon/ios/browser.png"/> 2.规格: iphone平台一般要求3种规格的图片:1x.2x.3x,也是就Icon ...

  8. get return value of python in shell

    from: https://stackoverflow.com/questions/2115615/assigning-value-to-shell-variable-using-a-function ...

  9. dpkg卸载

    from:https://jingyan.baidu.com/article/f54ae2fc2724a71e92b849c4.html 选择 dpkg -l来查看软件的状态. 选择 dpkg -P来 ...

  10. 尚硅谷redis学习10-复制

    是什么? 能干嘛? 怎么玩? 1) 初始情况 设置slave 日志查看 主机查看 备机日志 复制状态 觉见问题 1 切入点问题?slave1.slave2是从头开始复制还是从切入点开始复制?比如从k4 ...