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 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...
随机推荐
- K均值聚类
聚类(cluster)与分类的不同之处在于, 分类算法训练过程中样本所属的分类是已知的属监督学习. 而聚类算法不需要带有分类的训练数据,而是根据样本特征的相似性将其分为几类,又称为无监督分类. K均值 ...
- loadrunner:Action.c(4): Error -27796: Failed to connect to server "192.168.66.3:8080": [10060] Connection timed out
Action.c(4): Error -27796: Failed to connect to server "192.168.66.3:8080": [10060] Connec ...
- www.jqhtml.com 前端框架特效
www.jqhtml.com * 请选择课程 初级班 (PS.HTML.CSS.静态网站项目实战) 中级班 JavaScript基础.JavaScript DOM.jQuery.JS进阶.HTML5和 ...
- Java容器类源码分析前言之集合框架结构(基于JDK8)
一.基本概念 Java容器类库的用途是"保存对象",容器库类分为两个不同的分支. 1.Collection.可以保存一个或多个对象,将其保存为一个序列.Collection又可以细 ...
- Layui treeGrid
目前treeGrid的源码不是很完善, 没有开放, 只有社区里面有, 想用的可以看看下面方法: 1.加入treeGrid.js文件 (1)layui 的treeGrid 下载地址: https: ...
- 【转】pam_mysql - MySQL error (Can't connect to local MySQL server through socket
转自:http://350201.blog.51cto.com/340201/1034672 参照 http://wjw7702.blog.51cto.com/5210820/936244博 主做的p ...
- 三. Redis 主从复制
特点 1. Master可以拥有多个Slave 2. 多个Slave除可以连接一个Master外,还可以连接多个Salve(避免Master挂掉不能同步,当Master挂掉,其中一个Slave会立即变 ...
- Android--px(像素)和dp、sp之间的相互转化
public class DensityUtil { public DensityUtil() { } public static int dip2px(Context var0, float var ...
- Fiddler抓包使用教程-模拟低速网络环境
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/73467267 本文出自[赵彦军的博客] 在无线测试中,网络测试是必不可少的环节,通 ...
- vs2012碰到生成时报该错误:项目中不存在目标“GatherAllFilesToPublish”
手头一个vs2010升级到vs2012后,web项目发布到本地目录时项目报错:“该项目中不存在目标“GatherAllFilesToPublish”” 通过谷歌大神的帮助,找到了解决方法.共享之. 原 ...