UVa816 Abbott's Revenge
Abbott's Revenge
Time limit: 3.000 seconds
Abbott’s Revenge Abbott’s Revenge |
The 1999 World FinalsContest included a problem based on a “dicemaze.” At the time the problem was written, the judges were unable todiscover the original source of the dice maze concept. Shortly afterthe contest, however, Mr. Robert Abbott, the creator of numerous mazesand an author on the subject, contacted the contest judges andidentified himself as the originator of dice mazes. We regret that wedid not credit Mr. Abbott for his original concept in last year’sproblem statement. But we are happy to report that Mr. Abbott hasoffered his expertise to this year’s contest with his original andunpublished “walk-through arrow mazes.”
As are most mazes, awalk-through arrow maze is traversed by moving from intersection tointersection until the goal intersection is reached. As eachintersection is approached from a given direction, a sign near theentry to the intersection indicates in which directions theintersection can be exited. These directions are always left, forwardor right, or any combination of these.
Figure 1 illustrates awalk-through arrow maze. The intersections are identified as “(row,column)” pairs, with the upper left being (1,1). The “Entrance”intersection for Figure 1 is (3,1), and the “Goal” intersection is(3,3). You begin the maze by moving north from (3,1). As you walk from(3,1) to (2,1), the sign at (2,1) indicates that as you approach (2,1)from the south (traveling north) you may continue to go only forward.Continuing forward takes you toward (1,1). The sign at (1,1) as youapproach from the south indicates that you may exit (1,1) only bymaking a right. This turns you to the east now walking from (1,1)toward (1,2). So far there have been no choices to be made. This isalso the case as you continue to move from (1,2) to (2,2) to (2,3) to(1,3). Now, however, as you move west from (1,3) toward (1,2), you havethe option of continuing straight or turning left. Continuing straightwould take you on toward (1,1), while turning left would take you southto (2,2). The actual (unique) solution to this maze is the followingsequence of intersections: (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3)(1,2) (1,1) (2,1) (2,2) (1,2) (1,3) (2,3) (3,3).
You must write a programto solve valid walk-through arrow mazes. Solving a maze means (ifpossible) finding a route through the maze that leaves the Entrance inthe prescribed direction, and ends in the Goal. This route should notbe longer than necessary, of course. But if there are several solutionswhichare equally long, you can chose any of them.
Input
The input file willconsist of one or more arrow mazes. The first line of each mazedescription contains the name of the maze, which is an alphanumericstring of no more than 20 characters. The next line contains, in thefollowing order, the starting row, the starting column, the startingdirection, the goal row, and finally the goal column. All are delimitedby a single space. The maximum dimensions of a maze for this problemare 9 by 9, so all row and column numbers are single digits from 1 to9. The starting direction is one of the characters N, S, E or W,indicating north, south, east and west, respectively.
All remaining inputlines for a maze have this format: two integers, one or more groups ofcharacters, and a sentinel asterisk, again all delimited by a singlespace. The integers represent the row and column, respectively, of amaze intersection. Each character group represents a sign at thatintersection. The first character in the group is N, S, E or W toindicate in what direction of travel the sign would be seen. Forexample, S indicates that this is the sign that is seen when travellingsouth. (This is the sign posted at the north entrance to theintersection.) Following this first direction character are one tothree arrow characters. These can be L, F or R indicating left,forward, and right, respectively.
The list ofintersections is concluded by a line containing a single zero in thefirst column. The next line of the input starts the next maze, and soon. The end of input is the word END on a single line by itself.
Output
For each maze, theoutput file should contain a line with the name of the maze, followedby one or more lines with either a solution to the maze or the phrase“No Solution Possible”. Maze names should start in column 1, and allother lines should start in column 3, i.e., indented two spaces.Solutions should be output as a list of intersections in the format“(R,C)” in the order they are visited from the start to the goal,should be delimited by a single space, and all but the last line of thesolution should contain exactly 10 intersections.
The first maze in thefollowing sample input is the maze in Figure 1.
Sample Input |
Output for the Sample Input |
SAMPLE 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ELF * 2 3 SFR EL * 0 NOSOLUTION 3 1 N 3 2 1 1 WL NR * 1 2 NL ER * 2 1 SL WR NFR * 2 2 SR EL * 0 END |
SAMPLE (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1) (2,2) (1,2) (1,3) (2,3) (3,3) NOSOLUTION No Solution Possible |
Figure 1: An Example Walk-ThroughArrow Maze
Figure 2: Robert Abbott’s AtlantaMaze
Robert Abbott’swalk-through arrow mazes are actually For the |
ACM World Finals 2000, Problem A
【思路】
BFS。
一道BFS搜索最短路径的问题,与其他题目不同的是结点的转向有了限制,但也不算麻烦。
首先根据输入构造has_edge[x][y][dir][turn]数组,表示位于xy朝向dir能否向turn转向。然后以位置(x,y)方向dir为状态宽搜即可。
需要注意的是:
1、 以将sx sy向sdir方向移动一格为初始状态
2、 转向+移动算作一步。
【代码】
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std; const int maxn = ;
const int maxm=+;
const char* dirs="NESW";
const char* turns="FLR";
const int dx[]={-,,,};
const int dy[]={,,,-};
struct Node{
int x,y,dir;
};
int id_dirs(char c) {
return strchr(dirs,c)-dirs;
}
int id_turns(char c) {
return strchr(turns,c)-turns;
} bool has_edge[maxn][maxn][][];
int n,m,sx,sy,sdir,ex,ey; bool vis[maxn][maxn][];
Node p[maxn][maxn][];
void print(Node u) {
vector<Node> ans;
for(;;) {
ans.push_back(u);
if(u.x==sx && u.y==sy && u.dir==sdir) break;
u=p[u.x][u.y][u.dir];
}
ans.push_back((Node){sx-dx[sdir],sy-dy[sdir],sdir});
int cnt=;
for(int i=ans.size()-;i>=;i--) {
if(cnt%==) putchar(' ');
printf(" (%d,%d)",ans[i].x,ans[i].y);
if(++cnt%==) putchar('\n');
}
if(ans.size()%!=) putchar('\n');
}
Node walk(Node u,int i) {
if(i==) { u.dir=(u.dir+)%; }
if(i==) { u.dir=(u.dir+)%; }
return (Node){u.x+dx[u.dir],u.y+dy[u.dir],u.dir} ;
}
bool inside(int x,int y) {
return x> && x<= && y> && y<=;
}
void BFS() {
memset(vis,,sizeof(vis));
queue<Node> q;
q.push((Node){sx,sy,sdir});
vis[sx][sy][sdir]=;
while(!q.empty()) {
Node u=q.front(); q.pop();
if(u.x==ex && u.y==ey) { print(u); return ; }
for(int i=;i<;i++) {
Node v=walk(u,i);
if(has_edge[u.x][u.y][u.dir][i] && inside(v.x,v.y) && !vis[v.x][v.y][v.dir]) {
vis[v.x][v.y][v.dir]=;
p[v.x][v.y][v.dir]=u;
q.push(v);
}
}
}
printf(" No Solution Possible\n");
} int main() {
char T[maxm];
while(scanf("%s",&T))
{
char s[maxm];
if (scanf("%d%d%s%d%d",&sx,&sy,&s,&ex,&ey) != ) break;
sdir=id_dirs(s[]);
sx += dx[sdir] , sy += dy[sdir];
memset(has_edge,,sizeof(has_edge));
int x,y;
while(scanf("%d",&x) && x) {
scanf("%d",&y);
while(scanf("%s",&s) && s[]!='*') {
int dir=id_dirs(s[]);
for(int i=;i<strlen(s);i++) has_edge[x][y][dir][id_turns(s[i])]=;
}
}
printf("%s\n",T);
BFS();
}
return ;
}
UVa816 Abbott's Revenge的更多相关文章
- UVA816 Abbott's Revenge (三元组BFS)
题目描述: 输入输出: 输入样例: SAMPLE 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ...
- L - Abbott's Revenge(比较复杂的bfs)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UV ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- 【例题 6-14 UVA-816】Abbott's Revenge
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 预处理出某个方向的左边.前边.右边是哪个方向就好了. 然后就是普通的bfs了. hash存到某个点,走到这里的方向的最小距离. df ...
- UVA816 Abbott的复仇 Abbott's Revenge
以此纪念一道用四天时间完结的题 敲了好几次代码的出错点:(以下均为正确做法) memset初始化 真正的出发位置必须找出. 转换东西南北的数组要从0开始. bfs没有初始化第一个d 是否到达要在刚刚取 ...
- Abbott's Revenge UVA - 816 (输出bfs路径)
题目链接:https://vjudge.net/problem/UVA-816 题目大意: 有一个最多包含9*9 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...
- uva 816 abbott's revenge ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAN5CAYAAABqtx2mAAAgAElEQVR4nOy9sY4jydKezVuoayhH0r
- UVa (一道比较复杂的广搜) 816 Abbott’s Revenge
题意: 给出一个迷宫,在迷宫的节点处,面向某个方向只能向给定的方向转弯.给出起点和终点输出迷宫的最短路径,这里指的是刚刚离开起点的时刻,所以即使起点和终点重合路径也非空. 分析: 用三个变量来表示状态 ...
- UVA 816 Abbott’s Revenge
bfs求最短路,递归打印最短路的具体路径: 难点: 当前状态和转弯方式很复杂,要仔细处理: 递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的 ...
随机推荐
- 用原生js实现一个页面乘法口诀表
今天我自己用js实现了一个页面乘法口诀表(如图)来共享给大家,做的不是很好,如果大家有新的想法可以跟我交流哦. 代码如下: <!doctype html><html lang=&qu ...
- H5非主体结构元素
1.header元素:页面中一个内容区块或整个页面的标题: 具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,也可以包含数据表格.搜索表单或相关的logo图片. 一个网页 ...
- ubuntu使用github
Ubuntu下安装Git Ubuntu12.04 LTS默认是已经安装Git的,可以使用 git --version 测试是否安装.如果没有安装,使用命令: sudo apt-get install ...
- Amazon's NoSQL Journey and AWS Operations
AWS: Amazon Web Services 提供了一整套基础设施和应用程序服务,使您几乎能够在云中运行一切应用程序:从企业应用程序和大数据项目,到社交游戏和移动应用程序. 计算类: EC2:弹性 ...
- 简单安装python的pip工具模块
下载最新pip安装包 https://pypi.python.org/pypi/pip#downloads 安装 .tar.gz cd pip-.tar.gz python setup.py inst ...
- net Core 通过 Ef Core 访问、管理Mysql
net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...
- 学习Swift -- 构造器(中)
构造器(中) 值类型的构造器代理 构造器可以通过调用其它构造器来完成实例的部分构造过程.这一过程称为构造器代理,它能减少多个构造器间的代码重复. 构造器代理的实现规则和形式在值类型和类类型中有所不同. ...
- VS2012编译生成XP可以执行的程序
首先需要的就是下载VS2012的Update 4更新包,然后打开项目的属性页,在 配置属性->平台工具集 选项中选择 Visual Studio 2012 - Windows XP (v110_ ...
- 一些有用的 Emacs 配置(窗口快速切换、一键透明效果、任意位置删除整行等)
本篇文章记录的是一些有用的 Emacs 配置,有些是自己原创,有些是借鉴别人(能记起来出处的我放了链接). 规定:C 代表 Ctrl,M 代表 Alt. 1.设置一次跳跃 n 行的快捷键 按 C-M- ...
- Codeforces Round #205 (Div. 2) : D
思维题,感叹自己的智商不够啊. 思路大概是这样的: 1.排在队伍前面的女生是不用换位置的: 2.女生在队伍中的顺序是不会变的: 3.最后一个女生稳定了则程序结束: 4.每个女生都有个初始位置和最终位置 ...