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> ...
随机推荐
- pubwin会员合并
此博文已移至爬不稳独立博客:www.pubwin2009.net连接:http://www.pubwin2009.net/index.php/post/15.html 我们说下过程(这里,我们要求两个 ...
- 符号三角形(hdu 2510 搜索+打表)
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 火星A+B..(不贴代码了)
还是A+B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- x位全排列(next_permutation)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- C++----练习--string 从文件中一个一个单词的读直到文件尾
从文件中读取单词.并每行显示一个: 1. #include<iostream> #include<string> #include<vector> int main ...
- Linux的环境变量配置
以下都是环境变量相关的配置文件 1)/etc/enviroment 是系统的环境变量. (2)/etc/profile: 是所有用户的环境变量.当用户第一次登录时,该文件被执行. 并从/etc/pro ...
- 用日志文件备份sqlserver
USE [TestDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO )) as ) ),),)),)+ '.bak' backup dat ...
- UESTC_握手 CDOJ 913
握手 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status ...
- UESTC_In Galgame We Trust CDOJ 10
As we all know, there are many interesting (H) games in kennethsnow’s computer. But he sets a passwo ...
- iphone6闪存检测
iPhone6自从发布以后一直又不少的诟病和非议,比如一机难求,容易掰弯,程序崩溃等, 甚至传出了苹果将要召回这些问题设备,最近有人终于查出了iPhone6安装大量程序后崩溃的原因,原因就是大容量的i ...