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> ...
随机推荐
- sql server中主键列的插入问题
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.t_test'中的标识列指定显式值. SET IDENTITY_INSERT dbo.t_test ON ,'c' ...
- Delphi线程同步(临界区、互斥、信号量,包括详细代码)
当有多个线程的时候,经常需要去同步这些线程以访问同一个数据或资源. 例如,假设有一个程序,其中一个线程用于把文件读到内存,而另一个线程用于统计文件的字符数.当然,在整个文件调入内存之前,统计它的计数是 ...
- 新发现一个函数:GradientFill
位于Msimg32.dll之中 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144957(v=vs.85).aspx
- javascript之尺寸,位置,溢出
一.offsetWidth:元素的宽度,包括边框,内容,内边距. 二.offsetHeight:元素的高度,包括边框,内容,内边距. 三.offsetLeft:元素的X坐标(相对于最近已定位的祖先元素 ...
- curl 浏览器模拟请求实战
1,curl 常用选项
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...
- LD1-M(简单图的判定+构造,Havel定理)
题目链接 /* *题目大意: *给出一个图的每个点的度的序列,求能否构成一个简单图,如果能构出简单图,则输出图的邻接矩阵; * *算法思想: *Havel定理的应用; *给定一个非负整数序列{dn}, ...
- PHP MySQL Order By 关键词 之 Order By
ORDER BY 关键词 ORDER BY 关键词用于对记录集中的数据进行排序. 语法 SELECT column_name(s) FROM table_name ORDER BY column_na ...
- Unity扩展 自定义事件Send组件
在写项目的时候,我创建了一个方法里面需要一个int的参数. 我记得是UIEvent Trigger 不能直接传递一个数字,最多只能传递一个GameObject属性过去(=.=那个值不想再组件上定义) ...
- ng-cli
angluar2 cli 是一个比较好的工具 .解决 Angular 2 环境设置是一大入门门槛,有22%的人说环境设置太过复杂.Angular CLI的诞生,正是为了解决这个问题. 1. 基本介绍 ...