UVA 816 Abbott’s Revenge
bfs求最短路,递归打印最短路的具体路径;
难点:
当前状态和转弯方式很复杂,要仔细处理;
递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的上一个结点是确定的!)
ps:输出因为太懒不想处理所以按书上打的;递归打印理解有点麻烦。。。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std; struct node {
int x,y;
int w;
void init (int nx,int ny,int nw){
x=nx;
y=ny;
w=nw;
}
}p[][][]; int visit[][][];
int map[][][];
int dir[][][]={{-,,,-,,},
{,,-,,,},
{,,,,,-},
{,-,,,-,}
}; int id (char c){
if (c=='N'||c=='F')
return ;
else if (c=='E'||c=='L')
return ;
else if (c=='S'||c=='R')
return ;
else return ;
}
node ans[];
int tot;
int x0,y0,x1,y1,w1,sx,sy; void print (int x,int y,int w); int bfs (int x,int y,int w){
queue<node> q;
while (!q.empty ())
q.pop ();
node a,b;
a.init (x,y,w);
visit[a.x][a.y][a.w]=;
q.push (a);
while (!q.empty ()){
a=q.front ();//cout<<a.x<<" "<<a.y<<" "<<a.w<<endl;
q.pop ();
if (a.x==sx&&a.y==sy){
print (a.x,a.y,a.w);
return ;
}
int xx,yy,ww;
for (int i=;i<;i++){
xx=a.x;yy=a.y;ww=a.w;
xx+=dir[a.w][i][];
yy+=dir[a.w][i][];
if (i==)
ww=(ww+)%;
else if (i==)
ww=(ww+)%;
b.init (xx,yy,ww);
if ((map[a.x][a.y][a.w]&(<<i))){
if (xx<||xx>||yy<||yy>)
continue ;
if (visit[xx][yy][ww]>=)
continue ;
visit[xx][yy][ww]=visit[a.x][a.y][a.w]+;
p[xx][yy][ww]=a; //存储路径中的父结点
q.push (b);
} }
}
return ;
} void print (int x,int y,int w){
vector<node> v;
node a,b;
a.init (x,y,w);
v.push_back (a);
while (visit[a.x][a.y][a.w]){
a=p[a.x][a.y][a.w];
v.push_back (a);
}
a.init (x0,y0,w1);
v.push_back (a); int cnt=;
for (int i=v.size()-;i>=;i--){
if (cnt%==) cout<<" ";
cout<<" ("<<v[i].x<<","<<v[i].y<<")";
if (++cnt%==) cout<<endl;
}
if (v.size()%!=) cout<<endl;
} int main (){
char s[];
while (cin>>s){
if (strcmp (s,"END")==)
break ;
tot=;
memset (map,,sizeof map);
memset (visit,-,sizeof visit);
cout<<s<<endl;
char c;
cin>>x1>>y1>>c>>sx>>sy;
x0=x1;y0=y1;
w1=id (c);
x1+=dir[w1][][];
y1+=dir[w1][][];
int xx,yy;
while (cin>>xx&&xx){
cin>>yy;
gets (s);
int i=;
int j=id (s[]);
while (s[i++]!='*'){//cout<<i<<" "<<s[i];
if (s[i]==' '){
j=id (s[++i]);
continue ;
}
map[xx][yy][j]^=(<<id (s[i]));
}
//for (j=0;j<4;j++)
// cout<<map[xx][yy][j]<<endl;
}//cout<<xx<<endl;
if (!bfs (x1,y1,w1))
cout<<" No Solution Possible"<<endl;
}
return ;
}
UVA 816 Abbott’s Revenge的更多相关文章
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- Uva - 816 - Abbott's Revenge
这个迷宫问题还是挺好玩的,多加了一个转向的问题,有些路口不同的进入方式会有不同的转向限制,这个会比较麻烦一点,所以定义结点结构体的时候需要加一个朝向dir.总体来说是一道BFS求最短路的问题.最后打印 ...
- Uva 816 Abbott's Revenge(BFS)
#include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...
- UVA 816 Abbott's Revenge 紫书
紫书的这道题, 作者说是很重要. 但看着题解好长, 加上那段时间有别的事, 磨了几天没有动手. 最后,这道题我打了五遍以上 ,有两次被BUG卡了,找了很久才找到. 思路紫书上有,就缺少输入和边界判断两 ...
- UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...
- 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
题意: 给出一个迷宫,在迷宫的节点处,面向某个方向只能向给定的方向转弯.给出起点和终点输出迷宫的最短路径,这里指的是刚刚离开起点的时刻,所以即使起点和终点重合路径也非空. 分析: 用三个变量来表示状态 ...
- uva 816 - Abbott's Revenge(有点困难bfs迷宫称号)
是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...
随机推荐
- 实用AutoHotkey功能展示
AutoHotkey是什么 AutoHotkey是一个自动化脚本语言. AutoHotkey有什么用 可以让你用热键操控一切,操作电脑就像在表演魔术 我的口号 AutoHotkey!用过都说好! Au ...
- SQL触发器学习
简介 触发器是一种特殊类型的存储过程.触发器分为: DML( 数据操纵语言 Data Manipulation Language)触发器:数据库中表或视图的数据更改时触发,包括insert,upd ...
- hdu 1337 The Drunk Jailer
http://acm.hdu.edu.cn/showproblem.php?pid=1337 #include <cstdio> #include <cstring> #def ...
- Qt学习(四)—实例涂鸦画板mspaint
一.Qt图形绘制 自己在Qt开源社区在自学yafeilinux前辈的<Qt快速入门系列教程>中的图形篇,结合所学的知识,可以做一个涂鸦板实例 二.实现涂鸦板 1.新建工程mspaint, ...
- C# 集合性能 总结
一.引言 本文主要记录的是C#各种集合操作的性能,下面的标记说明描述标记的时间,下面的表格对比各种集合各种操作的时间. 标记说明: O(1) 表示无论集合中有多少项,这个操作需要的时间都不变,例如,A ...
- Php环境下载(PHPNow)安装
下载 From http://servkit.org/download 安装 解压下载包,双击setup.cmd,按照提示执行安装. 安装成功测试 原来的解压目录
- UVA_Cubic Eight-Puzzle UVA 1604
Let's play a puzzle using eight cubes placed on a 3 x 3 board leaving one empty square.Faces of cube ...
- iphone 6s pp助手 越狱
https://www.apple.com/iphone-6/合适初高中学习英语http://www.travelchinaguide.comhttp://jailbreak.25pp.com/ ip ...
- ubuntu中文实训手册
http://people.ubuntu.com/~happyaron/udc-cn/lucid-html/ http://www.apachefriends.org/zh_cn/xampp-linu ...
- 中秋佳节--理解Enum枚举
一.Enum枚举的作用 1.使用枚举可以限定取值范围,枚举中定义的每个常量都可以理解为对象: Eg: Public enum Color{ RED, GREEN,BULE; } 说明:RED实际上就表 ...