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 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...
随机推荐
- JavaWeb学习 (十九)————JavaBean
一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...
- Java基础之 运算符
前言:Java内功心法之运算符,看完这篇你向Java大神的路上又迈出了一步(有什么问题或者需要资料可以联系我的扣扣:734999078) 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,J ...
- Java设计模式学习记录-中介者模式
前言 中介者模式听名字就能想到也是一种为了解决耦合度的设计模式,其实中介者模式在结构上与观察者.命令模式十分相像:而应用目的又与结构模式“门面模式”有些相似.但区别于命令模式的是大多数中介者角色对于客 ...
- MVC 的 Razor引擎显示代码表达式与隐式代码表达式
隐式代码表达式 就是一个标识符,之后可以跟任意数量的方法调用("()").索引表达式("[]")及成员访问表达式(".").但是,除了在&q ...
- 【Spring】1、Spring 中的监听器 Listener
一.接口 1.EventListener 2.HttpSessionAttributeListener 继承EventListener接口 HttpSessionAttributeListener ...
- Java - BlockingQueue
https://juejin.im/post/5aeebd02518825672f19c546 https://www.infoq.cn/article/java-blocking-queue blo ...
- MAC MYSQ忘记密码重置方法
网友的方法,记个笔记请勿转载. step1: 关闭mysql服务: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server) ...
- Linux下查看tomcat控制台输出信息
1.进入tomcat/logs文件夹下 2.# tail -f catalina.out -f:实时刷新
- 【读书笔记】iOS-storyBoard-为一个按钮添加一个点击事件
按照故事板的用语,应用中的一个界面屏幕被称作一个”场景(Scene)",以后添加额外的场景时,停靠区中将有另一个部分. 一,新建立一个工程,如图所示. 二,选中Main.storyboard ...
- 语义分割的简单指南 A Simple Guide to Semantic Segmentation
语义分割是将标签分配给图像中的每个像素的过程.这与分类形成鲜明对比,其中单个标签被分配给整个图片.语义分段将同一类的多个对象视为单个实体.另一方面,实例分段将同一类的多个对象视为不同的单个对象(或实例 ...