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(搜索进阶)的更多相关文章

  1. UVa816 Abbott's Revenge

    Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge  Abbott’s Revenge The 1999 World FinalsC ...

  2. J - Abbott's Revenge 搜索 寒假训练

    题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...

  3. 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 ...

  4. L - Abbott's Revenge(比较复杂的bfs)

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UV ...

  5. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  6. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  7. 【例题 6-14 UVA-816】Abbott's Revenge

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 预处理出某个方向的左边.前边.右边是哪个方向就好了. 然后就是普通的bfs了. hash存到某个点,走到这里的方向的最小距离. df ...

  8. UVA816 Abbott的复仇 Abbott's Revenge

    以此纪念一道用四天时间完结的题 敲了好几次代码的出错点:(以下均为正确做法) memset初始化 真正的出发位置必须找出. 转换东西南北的数组要从0开始. bfs没有初始化第一个d 是否到达要在刚刚取 ...

  9. Abbott's Revenge UVA - 816 (输出bfs路径)

    题目链接:https://vjudge.net/problem/UVA-816 题目大意: 有一个最多包含9*9 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...

随机推荐

  1. SpringBoot学习(一)—— web项目基础搭建

    首先我们在浏览器打开这个网站 https://start.spring.io/ 打开后可以看到以下页面 在这里我们可以快速搭建一个SpringBoot基础项目,填写和选择完相应的信息后,我们点击那个绿 ...

  2. python——字符串格式化

    字符串的基础位置预留(网上到处都是) Python用一个tuple将多个值传递给模板,每个值对应一个格式符 在模板和tuple之间,有一个%号分隔,它代表了格式化操作. 使用字典 当只有很少量的占位符 ...

  3. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

  4. FCKEditor的使用步骤

    在线发布信息难免要用到在线编辑器,下面就说下在线编辑器的使用步骤: 1.下载FCK,这个不说了 2.把ZZGSEditor文件夹放到网站根目录 3.把FredCK.FCKeditorV2.dll文件放 ...

  5. Lucene实战之关键字匹配多个字段

    前言 当我们输入关键字时希望可以支持筛选多个字段,这样搜索内容的覆盖率就会大一些. 匹配多个字段主要用 MultiFieldQueryParser类. 单一字段搜索 QueryParser parse ...

  6. SPI 驱动框架

    SPI 驱动框架 1. 枚举过程 drivers/spi/spi.c: spi_register_board_info /* 对于每一个spi_master,调用spi_match_master_to ...

  7. WebService学习概念总结

    概念总结:WebSerevice是一种跨编程语言和跨操作系统平台的远程调用技术传输协议:HTTP技术构成:XML+XSD,SOAP,WSDL    XML封装数据格式,解决数据表示问题    XSD定 ...

  8. 【redis】7、redis用法总结

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 一.redis优点 Redis支持数据的持久化,可以将内存中 ...

  9. VUE CLI 3.0 项目引入 Mock.js

    mockjs 官网:http://mockjs.com/ 之前没有使用过 mockjs 的同学,请参考官网文档,数据生成规则和方法的调用都有详细说明. 一.通过npm安装依赖包 1. 进入到项目目录, ...

  10. 读懂SAP Leonardo物联网平台

    读懂SAP Leonardo物联网平台 https://blog.csdn.net/weixin_42137700/article/details/81903290 本文比较系统.全面地介绍了SAP ...