UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路。
分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的下一个点当做初始状态
注意理解题意:进入交叉点的朝向与从哪个方向进交叉点正好相反
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , , -};
const int dc[] = {, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[];
bool edge[][][][];
int dis[][][];
const char* a = "ESWN";//与数组dr,dc方向对应
const char* b = "LFR";
struct Node{
int x, y, d;//当前点的坐标,及进入该点的朝向
Node(){}
Node(int xx, int yy, int dd):x(xx), y(yy), d(dd){}
}num[][][];//记录该点的上一个点
int dir_id(char c){
return strchr(a, c) - a;
}
int turn_id(char c){
return strchr(b, c) - b;
}
bool judge(int x, int y){
return x >= && x <= && y >= && y <= ;
}
Node next(const Node&t, int turn){//turn与字符数组b下标对应
int dir = t.d;//向前走朝向不变
if(!turn){//向左走
dir = (dir + ) % ;//减1等价于加3
}
else if(turn == ){
dir = (dir + ) % ;
}
return Node(t.x + dr[dir], t.y + dc[dir], dir);
}
void print_ans(Node t, int tx, int ty, int dir){
stack<Node> st;
while(){
st.push(t);
if(dis[t.x][t.y][t.d] == ) break;
t = num[t.x][t.y][t.d];
}
st.push(Node(tx, ty, dir));
int cnt = ;
while(!st.empty()){
Node tmp = st.top();
st.pop();
++cnt;
if(cnt % == ) printf(" ");
printf(" (%d,%d)", tmp.x, tmp.y);
if(cnt % == ) printf("\n");
}
if(cnt % != ) printf("\n");
}
bool bfs(int sx, int sy, int dir, int ex, int ey){
memset(dis, -, sizeof dis);
queue<Node> q;
q.push(Node(sx, sy, dir));
dis[sx][sy][dir] = ;
int tx = sx - dr[dir];//迷宫起点
int ty = sy - dc[dir];
while(!q.empty()){
Node t = q.front();
q.pop();
if(t.x == ex && t.y == ey){
print_ans(t, tx, ty, dir);
return true;
}
for(int i = ; i < ; ++i){
Node v = next(t, i);
if(edge[t.x][t.y][t.d][i] && judge(v.x, v.y) && dis[v.x][v.y][v.d] < ){
dis[v.x][v.y][v.d] = dis[t.x][t.y][t.d] + ;
num[v.x][v.y][v.d] = t;
q.push(v);
}
}
}
return false;
}
int main(){
while(scanf("%s", s) == ){
if(strcmp(s, "END") == ) return ;
printf("%s\n", s);
memset(edge, , sizeof edge);
int sx, sy, ex, ey;
char dir;
scanf("%d%d %c%d%d", &sx, &sy, &dir, &ex, &ey);
int d = dir_id(dir);//进起点的下一个点的朝向
int nx = sx + dr[d];//起点的下一个位置
int ny = sy + dc[d];
int x;
while(scanf("%d", &x) == ){
if(x == ) break;
int y;
scanf("%d", &y);
while(scanf("%s", s) == ){
if(s[] == '*') break;
int tmp_dir = dir_id(s[]);
int len = strlen(s);
for(int i = ; i < len; ++i){
int tmp_turn = turn_id(s[i]);
edge[x][y][tmp_dir][tmp_turn] = ;
}
}
}
bool ok = bfs(nx, ny, d, ex, ey);//起点的下一个点因为已知,所以将其当为开始的点
if(!ok){
printf(" No Solution Possible\n");
}
}
return ;
}
UVA - 816 Abbott's Revenge(bfs)的更多相关文章
- UVa 816 Abbott的复仇(BFS)
寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...
- UVA 816 - Abbott's Revenge(BFS)
UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- UVA 816 Abbott’s Revenge
bfs求最短路,递归打印最短路的具体路径: 难点: 当前状态和转弯方式很复杂,要仔细处理: 递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的 ...
- Uva 816 Abbott的复仇(三元组BFS + 路径还原)
题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...
- Uva 816 Abbott's Revenge(BFS)
#include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Uva - 816 - Abbott's Revenge
这个迷宫问题还是挺好玩的,多加了一个转向的问题,有些路口不同的进入方式会有不同的转向限制,这个会比较麻烦一点,所以定义结点结构体的时候需要加一个朝向dir.总体来说是一道BFS求最短路的问题.最后打印 ...
随机推荐
- 任意promise串行执行算法 - 童彪
// 任意promise串行执行算法 - 童彪 function runAllPromise() { var p1 = new Promise((resove, reject) => { s ...
- ABC155E - Payment
简述题意,给你一个大数,你可以选择10的次幂进行加减运算,问如何用最少的次数从0到达这个大数 考虑从这个大数到0,从最低位开始,每次都将这个位置取完,2种策略,贪心的话不好处理进位的情况,可以想到是D ...
- Manacher 算法学习笔记
算法用处: 解决最长回文子串的问题(朴素型). 算法复杂度 我们不妨先看看其他暴力解法的复杂度: \(O(n^3)\) 枚举子串的左右边界,然后再暴力判断是否回文,对答案取 \(max\) . \(O ...
- 一文解读SDN (转)
一. 什么是SDN? SDN字面意思是软件定义网络,其试图摆脱硬件对网络架构的限制,这样便可以像升级.安装软件一样对网络进行修改,便于更多的APP(应用程序)能够快速部署到网络上. 如果把现有的网络看 ...
- 「CSP-S」2019年第一届Day1游记+题解
「CSP-S」2019年第一届Day1游记+题解 Day 1 7:30 A.M. 8:10 A.M. 8:30 A.M. T1 格雷码 题目 考场经历+思考(正解) 8:50 A.M. T2 括号树 ...
- TCP/IP,三次握手四次挥手,TCP/UDP , HTTP/HTTPS
internet:通用名词,由多个计算机网络组成的网络,网络间的通信协议是任意的 Internet:专用名词,当前全球最大的开放计算机网络,采用TCP/IP协议族作为通信的规则.www万维网是广泛应用 ...
- java并发:初探消费者和生产者模式
消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...
- AJAX封装数据处理简单操作
数据的封装处理主要展现在JS中,在页面里面引入封装的JS, "js/ajax.js" 简单封装将get和post方法都写入,get的方法和post的方法依然需要严格区分,包括typ ...
- 「NOIP2016」愤怒的小鸟
传送门 Luogu 解题思路 首先这个数据范围十分之小啊. 我们考虑预处理出所有可以带来贡献的抛物线 三点确定一条抛物线都会噻 然后把每条抛物线可以覆盖的点状压起来,然后状压DP随便转移就好了. 有一 ...
- SPringBootJPA的使用快速开发
一文搞懂如何在 Spring Boot 中正确使用 JPA JPA 这部分内容上手很容易,但是涉及到的东西还是挺多的,网上大部分关于 JPA 的资料都不是特别齐全,大部分用的版本也是比较落后的.另外, ...