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

  本题思路:保存起点和终点的状态,保存每个顶点的状态,包括每个方向的可转向方向,然后直接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. Spring Cloud (4)zool 路由网关

    一:作用: 1.1  转发 1.2  过滤 二.pom 三.启动类 ------------------------------------------------------------------ ...

  2. beego注解路由 [自定义方法]

    背景: beego生成的controller里面,默认get请求到由Get()方法处理:post请求由Post()方法处理 etc. 如果想自定义方法来处理请求,改怎么做? 直接拿beego的文档来说 ...

  3. Activity工作流学习(二)--Activity数据库

    23张表 ACT_RE_资源库流程规划表 act_re_deployment 部署信息表 act_re_model 流程设计模型部署表 act_re_procdef 流程定义数据表 ACT_RU_运行 ...

  4. 字符串md5之后转成int类型, 方便数据库索引

    function hashStringToInt($string){ $stringHash = substr(md5($string), 0, 8); return base_convert($st ...

  5. mybatis 异常和注意

    1.  Could not set parameters for mapping like语句出错,因将%%写入到mapper.xml中导致,将%%随同参数一并传入. 例:String userNam ...

  6. APP-10-文字识别-票据识别

    1.Postman测试 2.参数 https://cloud.baidu.com/doc/OCR/OCR-API.html#.E9.80.9A.E7.94.A8.E7.A5.A8.E6.8D.AE.E ...

  7. (原)Echarts 报Uncaught Error: Initialize failed: invalid dom 根本解决

    1.循环出的Echarts出现 Uncaught Error: Initialize failed: invalid dom ,附上完美解决方案 setTimeout(function () { co ...

  8. vue.js 组件引用之初级

    1. 构造组件,及组件引用:1.1 构造一个组件,1.2 注册一个组件,1.3  实例化Vue()即引用Vue() <!DOCTYPE html> <html lang=" ...

  9. Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter

    类的继承关系 Reader |__ BufferedReader.StringReader.InputStreamReader |__ FileReader Writer |__ BufferedWr ...

  10. How to Pronounce Work vs. Walk

    How to Pronounce Work vs. Walk Share Tweet Share Tagged With: Comparison If you’re confused about th ...