UVA816-Abbott's Revenge(搜索进阶)
Problem UVA816-Abbott's Revenge
Accept: 1010 Submit: 10466
Time Limit: 3000 mSec
Problem Description
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 years problem statement. But we are happy to report that Mr. Abbott has offered his expertise to this years 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.
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
Sample Input
SAMPLE
3 1 N 3 1
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
NOSOLUTUION
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 output
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
题解:加了一些限制条件的BFS,总体来说感觉在考察基本功。如何方便地把字符映射成int类型,如何记录路径,都是一些比较简单的东西,合在一起让这个题略显复杂,一点一点分析,code就好
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int maxn = ; struct Point{
int x,y;
int towards;
Point(int x = ,int y = ,int towards = ) :
x(x),y(y),towards(towards) {}
}; int gra[maxn][maxn][][];
bool vis[maxn][maxn][];
Point pre[maxn][maxn][];
int sx,sy,ex,ey;
char Dir[] = {'N','W','S','E'};
char To[] = {'F','L','R'};
int dr[][] = {{-,},{,-},{,},{,}};
Point start; void output(Point v){
vector<Point> ans;
ans.clear();
while(true){
ans.push_back(v);
//printf("(%d,%d)\n",v.x,v.y);
v = pre[v.x][v.y][v.towards];
if(v.towards == -){
ans.push_back(v);
break;
}
}
int cnt = ;
for(int i = ans.size()-;i >= ;i--){
if(cnt% == ) printf(" ");
printf(" (%d,%d)",ans[i].x,ans[i].y);
if(++cnt% == ) printf("\n");
}
if(ans.size()% != ) printf("\n");
} void bfs(){
queue<Point> que;
que.push(start);
pre[start.x][start.y][start.towards] = Point(sx,sy,-);
while(!que.empty()){
Point first = que.front();
que.pop();
int x = first.x,y = first.y,to = first.towards;
//printf("x:%d y:%d to:%d\n",x,y,to);
if(x==ex && y==ey){
output(first);
return;
}
for(int i = ;i < ;i++){
Point Next;
if(gra[x][y][to][i]){
if(i == ){
Next.towards = (to+)%;
}
else if(i == ) Next.towards = (to+)%;
else Next.towards = to;
Next.x = x+dr[Next.towards][],Next.y = y+dr[Next.towards][];
if(vis[Next.x][Next.y][Next.towards]) continue;
pre[Next.x][Next.y][Next.towards] = first;
que.push(Next);
vis[Next.x][Next.y][Next.towards] = true;
}
}
}
printf(" No Solution Possible\n");
} int main()
{
//freopen("input.txt","r",stdin);
char str[];
while(scanf("%s",str)){
if(!strcmp(str,"END")) break;
memset(vis,false,sizeof(vis));
memset(gra,,sizeof(gra));
memset(pre,,sizeof(pre));
printf("%s\n",str);
scanf("%d%d%s%d%d",&sx,&sy,str,&ex,&ey);
int dir = strchr(Dir,str[])-Dir;
start = Point(sx+dr[dir][],sy+dr[dir][],dir);
vis[start.x][start.y][start.towards] = true;
int x,y;
while(scanf("%d",&x) && x){
scanf("%d",&y);
while(~scanf("%s",str) && str[]!='*'){
int dir = strchr(Dir,str[])-Dir;
for(int i = ;i < (int)strlen(str);i++){
gra[x][y][dir][strchr(To,str[i])-To] = ;
}
}
}
bfs();
}
return ;
}
UVA816-Abbott's Revenge(搜索进阶)的更多相关文章
- UVa816 Abbott's Revenge
Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge Abbott’s Revenge The 1999 World FinalsC ...
- J - Abbott's Revenge 搜索 寒假训练
题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...
- 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 ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- 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 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...
随机推荐
- Java设计模式学习记录-命令模式
前言 这次要介绍的是命令模式,这也是一种行为型模式.最近反正没有面试机会我就写博客呗,该投的简历都投了.然后就继续看书,其实看书也会给自己带来成就感,原来以前不明白的东西,书上已经给彻底的介绍清楚了, ...
- sqlite+ef+powertools
1,安装驱动以便支持VS2010/VS2012设计时 VS2010, http://system.data.sqlite.org/downloads/1.0.88.0/sqlite-netFx40-s ...
- 聚簇索引(聚集索引) 不可作用于GUID类型字段
聚簇索引(又名聚集索引) 不可作用于GUID类型字段,即:数据库中不要把GUID类型作为主键或设置为聚集索引. 聚集索引:索引的物理存储顺序与数据表中行数据物理存储顺序一致(索引存储物理有序). 非聚 ...
- Why does the C# compiler translate this != comparison as if it were a > comparison?
Question: I have by pure chance discovered that the C# compiler turns this method: static bool IsNot ...
- springMVC 拦截器源码解析
前言:这两天学习了代理模式,自然想到了 springmvc 的 aop 使用的就是动态代理,拦截器使用的就是 jdk 的动态代理.今天看了看源码,记录一下.转载请注明出处:https://www.cn ...
- docker swarm 搭建及跨主机网络互连案例分析
准备工作 安装docker,不建议直接使用Docker官方的yum install docker wget http://yum.dockerproject.org/repo/main/centos/ ...
- hadoop的企业优化
前言: Mapreduce程序的效率的瓶颈在于两点: 计算机性能: CPU.内存.磁盘健康.网络 I/O操作: 数据倾斜 map和reduce数量设置不合理 map的运行时间太长,导致reduc的等待 ...
- JAVA的高并发基础认知 一
一.多线程的基本知识 1.1进程与线程的介绍 程序运行时在内存中分配自己独立的运行空间,就是进程 线程:它是位于进程中,负责当前进程中的某个具备独立运行资格的空间. 进程是负责整个程序的运行,而线程是 ...
- The open source JavaScript graphing library that powers Plotly
https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...
- 前端整理——Vue部分
(1)Vue的生命周期 1)创建vue实例,初始化生命周期钩子函数 2)数据检测及方法和计算属性代理.在数据检测和初始化数据之前调用beforeCreated(),这时还获取不到props或者data ...