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 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...
随机推荐
- openssl签署和自签署证书的多种实现方式
openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.采用自定义配置文件的实现方法 1.1 自建CA 自建CA的机制:1.生成 ...
- SQL语句简单笔记
Create database database name://创建数据库 Show databases dbName: //显示所有数据库 Create table tableName: //创建表 ...
- 以杨辉三角为例,从内存角度简单分析C语言中的动态二维数组
学C语言,一定绕不过指针这一大难关,而指针最让人头疼的就是各种指向关系,一阶的指针还比较容易掌握,但一旦阶数一高,就很容易理不清楚其中的指向关系,现在我将通过杨辉三角为例,我会用四种方法从内存的角度简 ...
- html特殊字体显示
1.下载需要显示的ttf字体. 2.css样式调用. /* 微软雅黑 */ @font-face { font-family: microsoftyahei; src: url('${pageCont ...
- Centos7 firewalld 基本使用
Centos7 的防火墙 firewalld比较常见 简单介绍使用 详细介绍链接推荐: https://blog.csdn.net/buster_zr/article/details/806049 ...
- How does this enqueue function work?
Question: I'm having trouble understanding this line: rear->next = temp; in this queue function: ...
- extract-text-webpack-plugin 作用、安装、使用
作用:该插件的主要是为了抽离css样式,防止将样式打包在js中引起页面样式加载错乱的现象 安装:插件安装命令如下: npm install extract-text-webpack-plugin -- ...
- 【代码笔记】Web-HTML-头部
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!--ti ...
- python之编码和解码
编码: 1. ascii. 有: 数字, 字母, 特殊字符. 8bit 1byte 128 最前面是0 2. gbk. 包含: ascii, 中文(主要), 日文, 韩文, 繁体文字. 16bit, ...
- win7 x64 +vs2015 + cmake3.10.3编译opencv-3.4.1+opencv_contrib-3.4.1源码,并进行配置
简介: 一直以来都是在ubuntu下使用opencv,最近因为有<图像处理与模式识别>这门课,需要使用vs2015+opencv提交课程作业,因为opencv官方编译好的exe没有cont ...