L - Abbott's Revenge(比较复杂的bfs)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description

| Abbott’s Revenge |
The 1999 World Finals Contest included a problem based on a “dice maze.” At the time the problem was written, the judges were unable to discover the original source of the dice maze concept. Shortly after the contest, however, Mr. Robert Abbott, the creator of numerous mazes and an author on the subject, contacted the contest judges and identified himself as the originator of dice mazes. We regret that we did not credit Mr. Abbott for his original concept in last year’s problem statement. But we are happy to report that Mr. Abbott has offered his expertise to this year’s contest with his original and unpublished “walk-through arrow mazes.”
As are most mazes, a walk-through arrow maze is traversed by moving from intersection to intersection until the goal intersection is reached. As each intersection is approached from a given direction, a sign near the entry to the intersection indicates in which directions the intersection can be exited. These directions are always left, forward or right, or any combination of these.
Figure 1 illustrates a walk-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 you approach from the south indicates that you may exit (1,1) only by making 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 is also 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 have the option of continuing straight or turning left. Continuing straight would take you on toward (1,1), while turning left would take you south to (2,2). The actual (unique) solution to this maze is the following sequence 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 program to solve valid walk-through arrow mazes. Solving a maze means (if possible) finding a route through the maze that leaves the Entrance in the prescribed direction, and ends in the Goal. This route should not be longer than necessary, of course. But if there are several solutions which are equally long, you can chose any of them.
Input
The input file will consist of one or more arrow mazes. The first line of each maze description contains the name of the maze, which is an alphanumeric string of no more than 20 characters. The next line contains, in the following order, the starting row, the starting column, the starting direction, the goal row, and finally the goal column. All are delimited by a single space. The maximum dimensions of a maze for this problem are 9 by 9, so all row and column numbers are single digits from 1 to 9. The starting direction is one of the characters N, S, E or W, indicating north, south, east and west, respectively.
All remaining input lines for a maze have this format: two integers, one or more groups of characters, and a sentinel asterisk, again all delimited by a single space. The integers represent the row and column, respectively, of a maze intersection. Each character group represents a sign at that intersection. The first character in the group is N, S, E or W to indicate in what direction of travel the sign would be seen. For example, S indicates that this is the sign that is seen when travelling south. (This is the sign posted at the north entrance to the intersection.) Following this first direction character are one to three arrow characters. These can be L, F or R indicating left, forward, and right, respectively.
The list of intersections is concluded by a line containing a single zero in the first column. The next line of the input starts the next maze, and so on. The end of input is the word END on a single line by itself.
Output
For each maze, the output file should contain a line with the name of the maze, followed by 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 all other 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 the solution should contain exactly 10 intersections.
The first maze in the following sample input is the maze in Figure 1.
| Sample Input | Output for the Sample Input |
|---|---|
SAMPLE |
SAMPLE |

Figure 1: An Example Walk-Through Arrow Maze

Figure 2: Robert Abbott’s Atlanta Maze
| Robert Abbott’s walk-through arrow mazes are actually intended for large-scale construction, not paper. Although his mazes are unpublished, some of them have actually been built. One of these is on display at an Atlanta museum. Others have been constructed by the American Maze Company over the past two summers. As their name suggests these mazes are intended to be walked through. For the adventurous, Figure 2 is a graphic of |
ACM World Finals 2000, Problem A
time:32ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
struct node
{
int r,c,dir; //从哪个dir到达r,c
node(int r=,int c=,int dir=):r(r),c(c),dir(dir){}
};
node p[MS][MS][];
bool edge[MS][MS][][]; //从dir方向到达r,c,是否可以向FLR方向前进
int d[MS][MS][]; // 距离 也起防止重复访问的作用
const char *dirs="ESWN";
const char *turns="FLR"; int dd[][]={{,},{,},{,-},{-,}};
int r0,c0,r1,c1,r2,c2,dir; int dir_id(char c)
{
return strchr(dirs,c)-dirs;
} int turn_id(char c)
{
return strchr(turns,c)-turns;
}
node walk(const node &u,int turn)
{
int dir=u.dir;
if(turn==)
dir=(dir-+)%;
if(turn==)
dir=(dir+)%;
return node(u.r+dd[dir][],u.c+dd[dir][],dir);
} bool inside(int r,int c)
{
return r>=&&r<=&&c>=&&c<=;
} bool read_case()
{
char s[],s2[];
if(scanf("%s%d%d%s%d%d",s,&r0,&c0,s2,&r2,&c2)!=)
return false;
printf("%s\n",s);
dir=dir_id(s2[]);
r1=r0+dd[dir][];
c1=c0+dd[dir][];
memset(edge,,sizeof(edge));
while()
{
int r,c;
scanf("%d",&r);
if(r==)
break;
scanf("%d",&c);
while(scanf("%s",s)==&&s[]!='*')
{
for(int i=;i<(int)strlen(s);i++)
edge[r][c][dir_id(s[])][turn_id(s[i])]=;
}
}
return true;
} void print_node(node u)
{
vector<node > nodes;
while()
{
nodes.push_back(u);
if(d[u.r][u.c][u.dir]==)
break;
u=p[u.r][u.c][u.dir];
}
nodes.push_back(node(r0,c0,dir));
int cnt=;
for(int i=nodes.size()-;i>=;i--)
{
if(cnt%==)
printf(" ");
printf(" (%d,%d)",nodes[i].r,nodes[i].c);
if(++cnt%==)
printf("\n");
}
if(nodes.size()%!=)
printf("\n"); } void solve()
{
queue<node > que;
memset(d,-,sizeof(d));
node u(r1,c1,dir);
d[u.r][u.c][dir]=; //注意厨师起点
que.push(u);
while(!que.empty())
{
node u=que.front();
que.pop();
if(u.r==r2&&u.c==c2)
{
print_node(u);
return ;
}
for(int i=;i<;i++)
{
node v=walk(u,i);
if(edge[u.r][u.c][u.dir][i]&&inside(v.r,v.c)&&d[v.r][v.c][v.dir]<)
{
d[v.r][v.c][v.dir]=d[u.r][u.c][u.dir]+;
p[v.r][v.c][v.dir]=u;
que.push(v);
}
}
}
printf(" No Solution Possible\n");
} int main()
{
while(read_case())
solve();
return ;
}
L - Abbott's Revenge(比较复杂的bfs)的更多相关文章
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- UVa816 Abbott's Revenge
Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge Abbott’s Revenge The 1999 World FinalsC ...
- uva 816 - Abbott's Revenge(有点困难bfs迷宫称号)
是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...
- Abbott's Revenge UVA - 816 (输出bfs路径)
题目链接:https://vjudge.net/problem/UVA-816 题目大意: 有一个最多包含9*9 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...
- 算法入门经典第六章 例题6-14 Abbott的复仇(Abbott's Revenge)BFS算法实现
Sample Input 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 SF ...
- 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 ...
- UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...
- Uva 816 Abbott's Revenge(BFS)
#include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...
- 【紫书】【重要】Abbott's Revenge UVA - 816 bfs 复杂模拟 带方向参数的迷宫
题意:一个迷宫,每个交叉路口有一路标,限制了你从某方向进入该路口所能进入的路口. 题解:1.对于方向的处理:将node多增加一维dir,通过一个const 字符数组 加 上dir_id函数 以及一个方 ...
随机推荐
- log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.
The reason for the error is a new listener in Tomcat 6.0.24. You can fix this error by adding this l ...
- 使用logmnr方法找回被误删除Oracle的数据的脚本
俗话说,常在河边走,哪有不湿鞋的.作为一个经常与数据库打交道的程序员,偶尔不小心误删除或误操作的数据也是在所难免的.如果是Oracle数据库,这里给您介绍一种从日志中找回数据的办法,下面这个地址是我以 ...
- HDU 1561The more, The Better(树形DP)
HDU 1561 The more, The Better 题目大意就不说了 直接DP[i][j]表示i为跟节点的子树上攻克j个城堡的所能获得的最多宝物的数量 DP[fa][j] = MAX{DP[ ...
- POJ3080Blue Jeans(暴力)
开始做字符串专题,地址 第一题水题,暴力就可以做 #include <map> #include <set> #include <stack> #include & ...
- newlsip 检查磁盘分区使用情况
主要还是用df -k这个命令,然后将输出结果全部逐行解析,最后调用REST API,发送给服务器保存. 参考代码: #!/usr/bin/newlisp (set 'cur-path "/o ...
- drop,truncate与delete的区别
注意:这里说的delete是指不带where子句的delete语句 相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据 不同点: 1. truncate和 d ...
- iOS多线程总结
1.不要同时开太多的线程(1~3条线程即可,不要超过5条) 2.线程概念 1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件 2> 子线程 : 后台线程,异步线程 3.不要 ...
- C# 浅拷贝与深拷贝
浅拷贝:给对象拷贝一份新的对象引用地址:(只是给一个对象多起了个名字,所以,当改变拷贝的某个属性的时候,原对象的对应属性亦会改变).浅拷贝的定义—— 只对值类型(或string)类型分配新的内存地址: ...
- 利用花生壳和IIS发布网页过程
老早利用做过类似的事情,但最近又忘了怎么弄的了,还是自己给自己总结下,省得以后到处找了. [动态域名绑定] 如果具有公网IP地址,申请一个免费的花生壳动态域名,再下一个花生壳客户,使用已经申请好的动态 ...
- JPA一对多和多对一关系
1-m:多的一方为关系维护端,关系维护端负责外键纪录的更新,关系被维护端没有权力更新外键纪录. 维护端注解 @OneToMany(cascade = { CascadeType.PERSIST, Ca ...